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?