0

I want to make all new commenters have a new account without them having to go through any registration process. That way if they use the same details again it will be stored as another comment they made.

I do not want to require them to "register" but instead register them automatically when they put their info into the comment form.

I'm using the default wordpress comment system without any plugins.

How can I make it so users are registered automatically when putting info into comment form to make a comment?

0

To achieve this, create a new page template and use this code in that file. Then, use your submit form to redirect the user after commenting to the new page.

$user_login = $_POST['user_login'];
$user_email = $_POST['user_email'];
$errors = register_new_user($user_login, $user_email);
if ( !is_wp_error($errors) ) {
    $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
    wp_safe_redirect( $redirect_to );
    exit();
}

Note that user_login and user_email are the names of your form's input boxes. The passwords will be randomly generated and sent to user's email address.

  • Actually this does not do what I requested. In fact it does what I said I didn't want to do - make the user log in. – rhinos Feb 10 '17 at 20:09
  • You can remove the redirect line if you don't want to, or change it to homepage or current page. – Jack Johansson Feb 11 '17 at 6:15
  • Alright then, you didn't mention these in the question. I suggest you go with @Spartacus answer, but you can replace wp_insert_user with register_new_user to automatically generate their password and send it to their email address. – Jack Johansson Feb 11 '17 at 6:49
  • Sparticus' answer doesn't work, sadly. – rhinos Feb 11 '17 at 21:14
-1

Hook into comment_post and use wp_insert_user():

add_action('comment_post','wpse_255850_reg_user',10,2);
function wpse_255850_reg_user($comment_ID, $comment_approved) {
    if( 1 === $comment_approved ){
    $userdata = array(
    'user_login' =>  sanitize_user($_POST['username']),
    'user_email'   =>  sanitize_email($_POST['user_email']),
    'user_pass'  =>  NULL,
    'role'  => 'subscriber',
    'show_admin_bar_front'  => false
                );

    $user_id = wp_insert_user( $userdata ) ;

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

       } //end if
    } //end function

The $_POST values in this code should reflect those of your actual comment form, the ones here are just generic. And make sure you thoroughly sanitize and validate your user inputs.

  • Looks good only one problem. password NULL how can user sign in again? – rhinos Feb 10 '17 at 19:12
  • Is there a password field on your comment form? – Spartacus Feb 10 '17 at 20:09
  • wpse_255850_reg_user is the function name I made up for this post. You can make it whatever you want. – Spartacus Feb 10 '17 at 20:10
  • Yeah, that would be a good solution to avoid the "registration" process, and to avoid a NULL password. – Spartacus Feb 10 '17 at 20:14
  • Well, I tried this today. It doesn't work. Will have to wait for someone else to give a working solution. Commentor is not registered. I inserted this into functions.php, cleared wp super cache, and left an anonymous comment. Sadly, the user was not registered even after approving comment. Just another anonymous user. – rhinos Feb 11 '17 at 21:13

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.