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.