アクティベーション・リンク・メールを自動的に再送信する方法

概要

ユーザがまだアカウントをアクティベートしていない場合、自動的にアクティベートメールを再送したいと思いませんか?PHPでこれを行う方法を紹介します。WPFormsのUser Registrationアドオンを使えば、登録フォームにユーザー有効化メールを送信できるようにすることができます。小さなスニペットを使うことで、これらのユーザーに有効化リンクを再送する関数を簡単に設定することができます。

セットアップ

まず、フォームを設定し、ユーザー登録タブでユーザーアクティベーションを有効にする必要があります。 この設定にヘルプが必要な場合は、こちらのドキュメントをご覧ください

フォーム設定のユーザー登録タブで、ユーザーアクティベーション設定を有効にします。

フォームを設定し、フィールドを追加したら、フォーム設定の ユーザー登録タブをクリックします。 次に、ユーザーアクティベーションを有効にするオプションをクリックします。

次に、このスニペットをサイトに追加します。スニペットをサイトに追加する方法についてサポートが必要な場合は、こちらのチュートリアルをご覧ください

/**
 * 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 );

最初の関数はそのステータスを探してユーザーアカウントを取得し、2番目の関数はWEEK_IN_SECONDSをスケジュール時間として自動的にメールを再送します。

WEEK_IN_SECONDSはWordPressの定数で、このタスクを毎週チェックするために使うことができます。もしこれをすぐに(あるいはそれ以降に)変更したい場合は、関数のこの部分を以下のいずれかに変更してください。

  • 秒単位
  • 時_秒
  • DAY_IN_SECONDS
  • 月_秒
  • 年_秒

WordPressが提供する期間定数の詳細については、WordPressのドキュメントを参照してください

必要なのはこれだけです!他のフォームで使用するユーザースマートタグを作成したいですか?チュートリアル「ユーザースマートタグをもっと作成する方法」をご覧ください。