form – activation email and reset password notification

It is possible to create a custom register form with Toolset User Forms, but it hasn’t an activation email feature.

A good solution to create an activation account email is to Auto-generate the user Password on the form, then, on form submit, send the user an email notification with a reset password link.

The default Toolset Forms notification includes the placeholder %%RESET_PASSWORD_LINK%%, but the output it’s a simple link to the lost-password page. It is possible to generate a real reset password link with custom code.

First of all it is necessary to check the checkbox “Auto-generate Password” on the Toolset form, it will assign an unknown random password to the WP User.

auto-generate-password

Then it is necessary to setup the reset password notification email with a custom function and then call it by cred_save_data action, where the $user_id equals the $post_id:

//Reset password email

function dtc_send_password_reset_mail($user_id){

    // Get user datas and generate the Reset Link
    $user = get_user_by('id', $user_id);
    $firstname = $user->first_name;
    $email = $user->user_email;
    $adt_rp_key = get_password_reset_key( $user );
    $user_login = $user->user_login;
    $rp_url = get_site_url().'/reset-password/?key=$adt_rp_key&login='.rawurlencode($user_login);
    $rp_link = '<a href="'. $rp_url.'">' . $rp_url . '</a>';

    // Set the email body and sebject
    $message = 'Hi '.$firstname.',<br>';
    $message .= 'Your user on '.get_bloginfo( 'name' ).' is: '.$user_login.'<br>';
    $message .= 'Click here to set the password for your account: <br>';
    $message .= $rp_link;

    $subject = __("Set your password and Activate your account on ".get_bloginfo( 'name'));

    // Set the email headers and send the email
    $headers = array();
    add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
    $headers[] = 'From: Your company name '."\r\n";
    wp_mail( $email, $subject, $message, $headers);

   // Reset content-type to avoid conflicts - http://core.trac.wordpress.org/ticket/23578
   remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}

add_action('cred_save_data', 'my_save_data',10,2);
function my_save_data($post_id, $form_data) {

  if ($form_data['id']==55) {

    $user_id = $post_id;
    
    //Send password reset to user
    dtc_send_password_reset_mail($user_id); 

  }
  
}


get_user_by() reference: https://developer.wordpress.org/reference/functions/get_user_by/
get_password_reset_key() reference: https://developer.wordpress.org/reference/functions/get_password_reset_key/
wp_mail() reference: https://developer.wordpress.org/reference/functions/wp_mail/

The custom function retrieves all the user’s data by get_user_by(‘id’, $user_id), then it generates the reset link with get_password_reset_key() and it creates the notification email and send it to the user with wp_mail().

Remember to change “$form_data[‘id’] == 55” with your Toolset User Form ID. It is also possible to customize the email ‘message’, the ‘subject’ and the ‘From’ header.

You can find this solution on this blog:

https://really-simple-plugins.com/sending-a-password-reset-link-for-php-added-wordpress-users/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>