AI要約
概要
ユーザーがまだアカウントを有効化していない場合に、ユーザーのアクティベーションメールを自動的に再送信しますか? 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 );
フォームから登録されたが、まだアカウントを有効化していないユーザーには、wpforms-pendingというステータスが付与されます。そのため、最初の関数はこのステータスを検索してこれらのユーザーアカウントを取得し、2番目の関数はWEEK_IN_SECONDSをスケジュール時間として使用してメールを自動的に再送信します。
WEEK_IN_SECONDSは、このタスクを毎週チェックするために使用できるWordPressの定数です。これをすぐに(またはそれ以降に)変更したい場合は、関数のこの部分を次のいずれかに変更できます。
- 秒単位の分
- 秒単位の時間
- 秒単位の日
- 秒単位の月
- 秒単位の年
WordPressが提供する時間期間の定数に関する詳細については、WordPress独自のドキュメントをご覧ください。
これで完了です!他のフォームで使用するユーザースマートタグを作成しますか?ユーザースマートタグの作成方法に関するチュートリアルをご覧ください。