<html lang="ja-jp" dir="ltr"><head></head><body>### [WPFormsユーザー登録アドオンの検証メッセージを変更する方法](https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/)

**公開日:** 2020年3月31日
**著者:** Umair Majeed

**抜粋:** このチュートリアルでは、WPFormsユーザー登録アドオンの検証メッセージを変更する方法を説明します。

**コンテンツ:**

ユーザー登録中に表示されるエラーメッセージをカスタマイズしたいですか？ WPFormsを使用すると、「ユーザー名が既に存在します」や「メールアドレスが既に存在します」のような、より役立つメッセージを作成して、訪問者が登録プロセスをスムーズに進められるようにガイドできます。

このガイドでは、PHPを使用してこれらの検証メッセージをカスタマイズする方法を説明します。

## 登録フォームの設定

まず、WPFormsの[ユーザー登録アドオン](https://wpforms.com/docs/how-to-install-and-use-user-registration-addon-with-wpforms/)がインストールされていることを確認してください。このアドオンには、ユーザーログインフォームとユーザー登録フォームの2つの既製テンプレートが用意されています。登録メッセージに焦点を当てるため、ユーザー登録テンプレートを使用して新しいフォームを作成します。

登録フォームの作成にヘルプが必要な場合は、[ユーザー登録フォームの作成方法](https://wpforms.com/docs/how-to-install-and-use-user-registration-addon-with-wpforms/)に関するガイドをご覧ください。

これらのフォームは、ユーザーをガイドするのに役立ついくつかの種類の検証メッセージを表示できます。各メッセージタイプをカスタマイズしましょう。

## 検証メッセージの変更

検証メッセージを変更するには、以下のいずれかのスニペットをサイトに追加する必要があります。

カスタムコードの追加方法がわからない場合は、[サイトにカスタムコードを追加する方法](https://wpforms.com/developers/how-to-add-custom-php-or-javascript-for-wpforms/)に関するガイドをご覧ください。

### ユーザー名が既に存在する場合のメッセージ

デフォルトのメッセージは「**A user with that username already exists.**」です。これをカスタマイズしてログインリンクを追加する方法は次のとおりです。

```

/**
 * ユーザー登録フォームのエラーメッセージ（ユーザー名が既に存在する場合）を変更します
 *
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 */
function wpf_dev_user_registration_username_exists( $msg ) {
    // ここに表示されるメッセージ
    $msg = __( 'A user with that username already exists. Please log in here 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.**」です。これをカスタマイズしてログインリンクを追加する方法は次のとおりです。

```

/**
 * ユーザー登録フォームのエラーメッセージ（メールアドレスが既に存在する場合）を変更します
 *
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 */
function wpf_dev_user_registration_email_exists( $msg ) {
    // ここに表示されるメッセージ
    $msg = __( 'A user with that username already exists. Please log in here 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 );
```

### パスワードが間違っている場合のメッセージ

デフォルトのメッセージは「**Error: The password you entered for the username is incorrect. Lost your password?**」です。これをカスタマイズしてパスワードリセットリンクを追加する方法は次のとおりです。

```

/**
 * パスワードが間違っています
 *
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 */
function wpf_dev_user_registration_login_error( $msg ) {
    // メールに表示されるメッセージ
    $msg = __('Sorry something went wrong! ', 'text-domain');
    $msg .= '';
    $msg .= __('Would you like to reset your password?', 'text-domain');
    $msg .= '';
    return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 1 );
```

PHPでURLを追加する場合、二重スラッシュでエスケープする必要があります。また、パスワードリセットURLを変更すると、ユーザーはWordPressの組み込みパスワードリセット機能を使用できなくなります。

### カスタムログインフォームメッセージ

ユーザーログインフォームのメッセージもカスタマイズできます。すべてのフォームに汎用的なメッセージを作成する方法は次のとおりです。

```

/**
 * ユーザーログインフォームのエラーメッセージを変更します
 *
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 */
function wpf_dev_user_registration_login_error($msg) {
    // メールに表示されるメッセージ
    $msg = __( 'Sorry something went wrong! ', 'text-domain' );
    $msg .= '';
    $msg .= __( 'Would you like to reset your password?', 'text-domain' );
    $msg .= '';
    return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 2 );
```

### 無効または期限切れのパスワードリセットリンクメッセージ

パスワードリセットテンプレートを使用しており、ユーザーが無効または期限切れのリセットリンクを開いたときに表示されるエラーメッセージを変更したい場合は、以下のスニペットを使用できます。

```

/**
 * WPFormsパスワードリセットフォームに表示されるエラーメッセージをカスタマイズします
 * リセットリンクが無効または期限切れの場合。
 *
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 *
 * @param string $message デフォルトのエラーメッセージ。
 * @return string
 */
function wpf_custom_password_reset_invalid_link_message( $message ) {
    return wp_kses(
        __( 'Error: Your password reset link is invalid or has expired. Please request a new link below.', 'text-domain' ),
        [ 'strong' =&gt; [] ]
    );
}
add_filter( 'wpforms_user_registration_frontend_reset_invalid_link_message', 'wpf_custom_password_reset_invalid_link_message', 10, 1 );
```

### フォーム固有のエラーメッセージ

フォームごとに異なるエラーメッセージを設定できます。フォームIDに基づいてメッセージをカスタマイズする方法は次のとおりです。フォームIDの見つけ方がわからない場合は、フォームIDとフィールドIDの見つけ方に関するガイドをご覧ください。

```

/**
 * ユーザーログインフォームのエラーメッセージをフォームごとに変更します
 *
 * @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 ) {
            // これはフォームID 525用です
            if ( $form_id === 525) {
                // これはフォームID 525の特定メッセージです
                return __( 'The error message here for form #525', 'text-domain' );
            }
            // これはフォームID 526用です
            if ( $form_id === 526) {
                // これはフォーム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**スニペットを最初にスニペットを追加した場合、フォームが送信されると、追加された順序でスニペットが実行され、最初に失敗したスニペットのメッセージが表示されます。

これで、ユーザー登録アドオンに追加の検証メッセージを追加するために必要なすべてが揃いました。登録が完了したらユーザーを自動的にログインさせたいですか？[登録後にユーザーを自動ログインさせる方法](https://wpforms.com/developers/automatically-log-in-users-after-registration/)に関するガイドを確認するか、登録フォームにカスタムユーザーメタフィールドを追加する方法を学びましょう。

#### Q: ユーザーがパスワードをリセットした後に表示される確認メッセージを変更できますか？

はい、できます。**パスワードリセット**テンプレートを使用しており、ユーザーがパスワードを正常にリセットした後に表示される確認メッセージをカスタマイズしたい場合は、以下のスニペットを使用できます。

```

function wpf_custom_reset_confirmation_message( $message, $form_data ) {
    // ユーザー名またはメールを取得するための特定のフィールドタイプが存在するかどうかに基づいて確認メッセージを変更します。
    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 );
```

## 参考フィルター

- [wpforms\_user\_registration\_process\_registration\_process\_user\_email\_exists\_error\_message](https://wpforms.com/developers/wpforms_user_registration_process_registration_process_user_email_exists_error_message/ "wpforms_user_registration_process_registration_process_user_email_exists_error_messageフィルターの使用方法")
- [wpforms\_user\_registration\_process\_login\_process\_wp\_error\_message](https://wpforms.com/developers/wpforms_user_registration_process_login_process_wp_error_message/ "wpforms_user_registration_process_login_process_wp_error_messageフィルターの使用方法")
- [wpforms\_user\_registration\_process\_registration\_process\_username\_exists\_error\_message](https://wpforms.com/developers/wpforms_user_registration_process_registration_process_username_exists_error_message/ "wpforms_user_registration_process_registration_process_username_exists_error_messageフィルターの使用方法")

**カテゴリー:** アドオン

**タグ:** PHP, ユーザー登録アドオン

---</body></html>