AI要約
ユーザー登録中に表示されるエラーメッセージをカスタマイズしますか? WPFormsを使用すると、「ユーザー名が既に存在します」や「メールアドレスが既に存在します」のような、より役立つメッセージを作成して、訪問者を登録プロセスに誘導できます。
このガイドでは、PHPを使用してこれらの検証メッセージをカスタマイズする方法を説明します。
登録フォームの設定
まず、WPFormsのユーザー登録アドオンがインストールされていることを確認してください。このアドオンには、ユーザーログインフォームとユーザー登録フォームの2つの事前作成済みテンプレートがあります。登録メッセージに焦点を当てているため、ユーザー登録テンプレートを使用して新しいフォームを作成してください。
これらのフォームは、ユーザーを誘導するのに役立ついくつかの種類の検証メッセージを表示できます。各メッセージの種類をカスタマイズしましょう。
検証メッセージの変更
検証メッセージを変更するには、次のいずれかのスニペットをサイトに追加する必要があります。
カスタムコードの追加方法がわからない場合は、サイトにコードスニペットを追加する方法に関するガイドを参照してください。
ユーザー名が既に存在する場合のメッセージ
デフォルトのメッセージはそのユーザー名のユーザーは既に存在します。です。カスタマイズしてログインリンクを追加する方法は次のとおりです。
/**
* 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 );
メールアドレスが既に存在する場合のメッセージ
デフォルトのメッセージはそのメールアドレスを持つユーザーは既に存在します。です。カスタマイズしてログインリンクを追加する方法は次のとおりです。
/**
* 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 );
無効または期限切れのパスワードリセットリンクメッセージ
パスワードリセットテンプレートを使用しており、ユーザーが無効または期限切れのリセットリンクを開いたときに表示されるエラーメッセージを変更したい場合は、次のスニペットを使用できます。
/**
* Customize the error message shown on the WPForms Password Reset form
* when the reset link is invalid or expired.
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*
* @param string $message The default error message.
* @return string
*/
function wpf_custom_password_reset_invalid_link_message( $message ) {
return wp_kses(
__( '<strong>Error:</strong> Your password reset link is invalid or has expired. Please request a new link below.', 'text-domain' ),
[ 'strong' => [] ]
);
}
add_filter( 'wpforms_user_registration_frontend_reset_invalid_link_message', 'wpf_custom_password_reset_invalid_link_message', 10, 1 );
フォーム固有のエラーメッセージ
フォームごとに異なるエラーメッセージを設定できます。フォームIDに基づいてメッセージをカスタマイズする方法は次のとおりです。フォーム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スニペットを追加した場合、フォームが送信されると、追加された順序でスニペットが実行され、最初に失敗したスニペットのメッセージが表示されます。
これで、ユーザー登録アドオンに追加の検証メッセージを追加するために必要なすべてが揃いました。ユーザーが登録を完了したら、自動的にログインさせますか?登録後にユーザーを自動ログインさせる方法に関するガイドを確認するか、カスタムユーザーメタフィールドを登録フォームに追加する方法を学びましょう。
Q: ユーザーがパスワードをリセットした後に表示される確認メッセージを変更できますか?
はい。パスワードリセットテンプレートを使用しており、ユーザーがパスワードを正常にリセットした後に表示される確認メッセージをカスタマイズしたい場合は、以下のスニペットを使用できます。
function wpf_custom_reset_confirmation_message( $message, $form_data ) {
// Modify the confirmation message based on the presence of a specific field type for grabbing the username or email.
if ( true === wpforms_has_field_type( 'text', $form_data ) ) {
$message = esc_html__( 'Please check your email to reset your password.', 'wpforms' );
} else {
$message = esc_html__( 'Your new password has been set successfully!', 'wpforms' );
}
return $message;
}
add_filter( 'wpforms_frontend_confirmation_message', 'wpf_custom_reset_confirmation_message', 10, 2 );