ユーザー登録時に表示されるエラーメッセージをカスタマイズしたいですか?WPFormsでは "Username Already Exists "や "Email Already Exists "のような親切なメッセージを作成することができます。
このガイドでは、PHP を使ってこれらの検証メッセージをカスタマイズする方法を説明します。
登録フォームの設定
まず、WPForms用のUser Registrationアドオンがインストールされていることを確認してください。このアドオンには2つのテンプレートが用意されています:User Login Form と User Registration Form です。ここでは登録メッセージに焦点を当てるので、User Registration テンプレートを使って新しいフォームを作成します。
これらのフォームでは、ユーザーをガイドするために、いくつかのタイプのバリデーションメッセージを表示することができます。それぞれのメッセージをカスタマイズしてみましょう。
バリデーション・メッセージの変更
バリデーション・メッセージを変更するには、以下のスニペットのいずれかをサイトに追加する必要があります。
カスタムコードの追加方法がわからない場合は、コードスニペットをサイトに追加する方法のガイドをご覧ください。
ユーザー名はすでに存在します
デフォルトのメッセージはA user with that username already existsです。このメッセージをカスタマイズし、ログインリンクを追加する方法は以下の通りです:
/**
* Change the User Registration Form error message for the already existing username
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_username_exists( $msg ) {
// This is the message that would appear
$msg = __( 'A user with that username already exists. Please <a href="http://yourdomain/wp-admin/">log in here</a> to your account.', 'text-domain' );
return $msg;
}
add_filter( 'wpforms_user_registration_process_registration_process_username_exists_error_message', 'wpf_dev_user_registration_username_exists', 10, 1 );
すでに存在する電子メールのメッセージ
デフォルトのメッセージはA user with that email already existsです。ここでは、ログインリンクを使用してカスタマイズする方法を説明します:
/**
* Change the User Registration Form error message for already existing email address
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_email_exists( $msg ) {
// This is the message that would appear
$msg = __( 'A user with that username already exists. Please <a href="http://yourdomain/wp-admin/">log in here</a> to your account.', 'text-domain' );
return $msg;
}
add_filter( 'wpforms_user_registration_process_registration_process_user_email_exists_error_message', 'wpf_dev_user_registration_email_exists', 10, 1 );
不正なパスワードのメッセージ
デフォルトのメッセージは「エラー」です:ユーザー名に入力したパスワードが間違っています。パスワードをお忘れですか?パスワードリセットリンクでカスタマイズする方法はこちら:
/**
* Password is incorrect
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_login_error( $msg ) {
// This is the message that would appear in the email
$msg = __('Sorry something went wrong! ', 'text-domain');
$msg .= '<a href=\\"http://www.website.com\\">';
$msg .= __('Would you like to reset your password?', 'text-domain');
$msg .= '</a>';
return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 1 );
カスタムログインフォームメッセージ
ユーザーログインフォーム用のメッセージもカスタマイズできます。ここでは、すべてのフォーム用の汎用メッセージを作成する方法を説明します:
/**
* Change the User Login Form error message
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_login_error($msg) {
// This is the message that would appear in the email
$msg = __( 'Sorry something went wrong! ', 'text-domain' );
$msg .= '<a href="'.esc_url( wp_lostpassword_url() ).'">';
$msg .= __( 'Would you like to reset your password?', 'text-domain' );
$msg .= '</a>';
return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 2 );
フォーム固有のエラーメッセージ
フォームごとに異なるエラーメッセージを設定することができます。ここではフォームIDに基づいてメッセージをカスタマイズする方法を説明します。フォーム ID を見つけるのにお困りの場合は、フォームとフィールド ID の見つけ方ガイドをご覧ください。
/**
* Change the User Login Form error message specific per form
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_form_login_errors( $fields, $entry, $form_data ) {
$form_id = (int) $form_data[ 'id' ];
add_filter(
'wpforms_user_registration_login_error',
function ( $message, $code ) use ( $form_id ) {
// This if for form ID 525
if ( $form_id === 525) {
// This is the specific message for form ID 525
return __( 'The error message here for form #525', 'text-domain' );
}
// This is for form ID 526
if ( $form_id === 526) {
// This is the specific message for form ID 526
return __( 'The error message here for form #526', 'text-domain' );
}
return $message;
},
10,
2
);
}
add_action( 'wpforms_process', 'wpf_dev_form_login_errors', 9, 3 );
よくある質問
Q: すべてのエラーメッセージを一度に表示することはできますか?
A:いいえ、PHPはスニペットが追加された順番、または関数に設定された優先順位で実行されます。wpforms_user_registration_username_existsスニペットを最初に追加した場合、フォームが送信されると、追加された順にスニペットが実行され、最初に失敗したスニペットのメッセージが表示されます。
以上で、ユーザー登録アドオンにバリデーションメッセージを追加する作業は完了です。登録が完了したユーザーを自動的にログインさせたいですか?登録後にユーザーを自動的にログインさせる方法や、登録フォームにカスタムユーザーメタフィールドを追加する方法については、ガイドをご覧ください。