我如何测试为什么评论挂钩不起作用?

时间:2017-02-19 作者:user6686780

我试图在评论获得批准后执行操作。此操作的目的是使用register_new_user, 使用评论者的姓名作为用户名,电子邮件作为用户电子邮件。

使用wordpress自动生成密码,并通过电子邮件向新用户发送其用户详细信息,我认为在使用register_new_user.

然而,这是行不通的。我的代码有问题,但我不知道是什么。

通常在javascript中,我可以使用alert对其进行测试。在css中,它也很容易测试。然而,我不知道如何测试这一点,因为在您批准评论之前,操作不会发生。

所以我需要知道的是,如何测试代码functions.php 甚至都在工作吗?

我的评论表(comments.php):

args = array(

    \'comment_field\' =>  \'<div class="form-group the-comment-form"><label for="comment" class="custom-avatar">\' . $useravatar_custom .

      \'</label><div id="\' . $textareaId . \'"><textarea id="comment" class="form-control" name="comment" cols="45" rows="8" aria-required="true" placeholder="Join the discussion..."></textarea></div></div>\',      

    \'fields\' => apply_filters( \'comment_form_default_fields\', array(

      \'author\' =>

        \'<div class="form-group comments-name">\' .

        \'<label for="author">\' . __( \'Username\', \'usertheme\' ) . \'</label> \' .

        ( $req ? \'<span class="required">*</span>\' : \'\' ) .

        \'<input id="author" name="author" class="form-control" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) .

        \'" size="30"\' . $aria_req . \' /></div>\',


      \'email\' =>

        \'<div class="form-group comments-email"><label for="email">\' . __( \'Email\', \'usertheme\' ) . \'</label> \' .

        ( $req ? \'<span class="required">*</span>\' : \'\' ) .

        \'<input id="email" name="email" class="form-control" type="text" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) .

        \'" size="30"\' . $aria_req . \' /></div>\'
我的代码输入functions.php:

function reg_anon_user_auto($comment_ID, $comment_approved) {
    if( 1 === $comment_approved ){
        $userdata = array(
            \'user_login\' =>  sanitize_user($_POST[\'author\']),
            \'user_email\'   =>  sanitize_email($_POST[\'email\']),
            \'role\'  => \'subscriber\',
            \'show_admin_bar_front\'  => false
        );

        $user_id = register_new_user( $userdata ) ;

        // On success.
        if ( ! is_wp_error( $user_id ) ) {
           wp_redirect( get_permalink() ); 
           exit;
        }
    } //end if
} //end function

add_action( \'comment_post\', array( $this, \'reg_anon_user_auto\' ), 10, 2 );
很明显,这样的事情是行不通的,因为您无法在php中发出警报:

function reg_anon_user_auto($comment_ID, $comment_approved) {
    if( 1 === $comment_approved ){
        $userdata = array(
            alert("yep it\'s working code")
        );

        $user_id = register_new_user( $userdata ) ;

        // On success.
        if ( ! is_wp_error( $user_id ) ) {
           wp_redirect( get_permalink() ); 
           exit;
        }
    } //end if
} //end function

add_action( \'comment_post\', array( $this, \'reg_anon_user_auto\' ), 10, 2 );
那么,我如何测试代码为什么不能工作呢?

1 个回复
SO网友:David Lee

要检查变量的值,可以使用var_dumpprint_r (包裹在<pre></pre> 看起来更好)您还可以启用debug for WordPress 和使用error_log 写入调试。日志

现在,要手动触发操作,请使用do_action:

do_action( \'comment_post\' );
考虑到您是手动触发的,因此您的案例中不存在预期的变量,您可能也需要手动发送它们,如下所示:

$comment_ID = 12;
$comment_approved = 1;
do_action( \'comment_post\' , $comment_ID, $comment_approved );
在你的案件中使用它们,var_dump 就像print_ralert 或aconsole.log 请阅读每个链接页,首先检查是否调用了您的函数:

function reg_anon_user_auto($comment_ID, $comment_approved) {
//Here we call var_dump to check if this function was called at all

var_dump("Test i was called");

    if( 1 === $comment_approved ){
        $userdata = array(
            \'user_login\' =>  sanitize_user($_POST[\'author\']),
            \'user_email\'   =>  sanitize_email($_POST[\'email\']),
            \'role\'  => \'subscriber\',
            \'show_admin_bar_front\'  => false
        );

        $user_id = register_new_user( $userdata ) ;

        // On success.
        if ( ! is_wp_error( $user_id ) ) {
           wp_redirect( get_permalink() ); 
           exit;
        }
    } //end if
} //end function
或者您可以使用print_r("Test i was called");

Fix to your code:

function reg_anon_user_auto($comment_ID, $comment_approved) {
    if (1 === $comment_approved) {
        $the_comment = get_comment( $comment_ID );//we get the comment

        $user_login = sanitize_user($the_comment->comment_author);//we get the author of the comment
        $user_email = sanitize_email($the_comment->comment_author_email);//we get the email of the comment

        $user_id = register_new_user($user_login,$user_email);//we create the new user

        // On success.
        if (!is_wp_error($user_id)) {
            wp_redirect(get_permalink());
            exit;
        }
    } //end if
}//end function
the\'comment_post\' 创建帖子时会触发操作,因此没有$_POST[\'author\'] 也没有$_POST[\'email\'], 您必须从comment object, 还有register_new_user 不接受数组及其2个字符串。