How to Automatically Resend User Activation Link Emails

Overview

Would you like to resend user activation emails automatically if the users haven’t already activated their accounts? We’ll show you how to do this with PHP. With the WPForms User Registration addon, you can enable the registration form to have user activation emails sent. Using a small snippet, you can easily set up a function that resends the activation links to these users, just set up the function and that’s all you have to do.

Setup

First, you’ll need to set up your form and enable user activation on the User Registration tab. If you need help in setting this up, please check out this documentation.

enable the user activation setting from the User Registration tab of the form settings.

Once you’ve set up your form and added your fields, click over to the User Registration tab under the form Settings. Then click the option to Enable user activation.

Next, add this snippet to your site. If you need any assistance in how to add snippets to your site, please check out this tutorial.

/**
 * Grab the users who have registered but not activated their account
 *
 * @link   https://wpforms.com/developers/how-to-automatically-resend-user-activation-link-emails/
 */

function user_registration_resend_activation() {

   $unapproved_users = get_users(
      [
         'meta_key'   => 'wpforms-pending',
         'meta_value' => true,
      ]
   );

   foreach ( $unapproved_users as $user ) {

      \WPFormsUserRegistration\SmartTags\Helpers\Helper::set_user( $user );

      wpforms_user_registration()->get( 'email_notifications' )->resend_activation( $user->ID );
   }
}

add_action( 'user_registration_resend_activation_action', 'user_registration_resend_activation', 10 );

/**
 * Automatically resend any emails to users who haven't activated their account
 *
 * @link   https://wpforms.com/developers/how-to-automatically-resend-user-activation-link-emails/
 */

function user_registration_resend_activation_as_task() {

   if ( ! function_exists( 'as_schedule_recurring_action' ) ) {
      return null;
   }

   if ( as_next_scheduled_action( 'user_registration_resend_activation_action' ) ) {
      return null;
   }

   
   as_schedule_recurring_action( strtotime( 'midnight tonight' ), WEEK_IN_SECONDS, 'user_registration_resend_activation_action' );
}

add_action( 'admin_init', 'user_registration_resend_activation_as_task', 10 );

Any users who have registered through your form but haven’t activated their account yet are given a status of wpforms-pending, so the first function will look for that status to fetch those user accounts and the second function will automatically resend the emails using the WEEK_IN_SECONDS as the scheduled time.

The WEEK_IN_SECONDS is a WordPress constant that you can use to check weekly for this task. If you’d like to change this to be soon (or even later) you could change this part of the function to any of the following.

  • MINUTE_IN_SECONDS
  • HOUR_IN_SECONDS
  • DAY_IN_SECONDS
  • MONTH_IN_SECONDS
  • YEAR_IN_SECONDS

For more information on the constants that WordPress provides for time periods, please check out their own documentation.

And that’s all you need! Would you like to create some user Smart Tags to use in other forms? Check out our tutorial on How to Create More User Smart Tags.