1

I am trying to perform an action after a comment is approved. What this action is, is to register the commenter automatically using register_new_user, using the commenter's name as the username, and email as the user email.

And a password generated automatically using wordpress and the new user is emailed their user details, which I think is supposed to happen when using register_new_user.

However, this is not working. Something is wrong with my code but I don't know what.

Normally in javascript I could test it using alert. In css it's also easy to test. However, I don't know how to test this since the action doesn't occur until you approved the comment.

So what I need to know is how can I test to see if the code in functions.php is even working at all?

My comment form (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>'

My code in 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 );

Obviously, something like this won't work since you can't alert in 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 );

So how can I test to see why my code isn't working?

1

To check the values of your variables you can use var_dump or print_r (wrapped in <pre></pre> looks better) you can also enable debug for WordPress and use error_log to write to the debug.log.

Now for the action to be triggered manually use do_action:

do_action( 'comment_post' );

take into account that you are triggering this manually so the expected variables dont exists in your case, you might need to send them manually too like this:

$comment_ID = 12;
$comment_approved = 1;
do_action( 'comment_post' , $comment_ID, $comment_approved );

To use them in your case, var_dump are like print_r an alert or a console.log please read the linked pages to each one, first of all check if your function is being called at all:

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

or you can use 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' action is triggered when a post is created, so there is no $_POST['author'] nor $_POST['email'], you have to get those values from the comment object, also the register_new_user doesnt accepts an array, its 2 strings.

  • $comment_ID will never be 12, so I don't know what you are suggesting to put there. The comment ID will depend on the comment being approved. – user6686780 Feb 19 '17 at 23:02
  • Also, changing add_action to do_action had no effect, user still not registered. Maybe you can elaborate more on what you mean by var_dump or print_r and how to use it in this context. – user6686780 Feb 19 '17 at 23:03
  • i am just pointing tools that you can use for debugging, let me add them to your context, try playing with it. – David Lee Feb 19 '17 at 23:25
  • Please post your code suggestion and I will try it to see if it works. – user6686780 Feb 19 '17 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.