制限文字バリデーションテキストの変更方法

フォームフィールドの下に表示される文字数制限のテキストを変更したい場合、特に「1行テキストまたは段落テキストの 文字数制限」フォームフィールドを使用している場合は、こちらをご覧ください。

デフォルトでは、フォームフィールドの下に、最大文字数50文字中0文字、または最大単語数50語中0文字など、フォームの設定に応じて、現在の文字数を示すメッセージが表示されます。このメッセージをパーソナライズして、残りの文字数を含めることができます。この調整はシンプルな PHP スニペットで簡単に実現できます。このチュートリアルでは、PHP を使ってこのメッセージを変更する方法を説明します。

フォームの作成

まず、フォームを作成し、フォーム・フィールドを追加します。

フォームの作成にヘルプが必要な場合は、こちらのドキュメントをご覧ください

新しいフォームにフィールドを追加する

段落テキスト・フォーム・フィールドを追加しました。

文字数制限の設定

次に、追加した段落テキストを選択し、「詳細設定」タブをクリックして「長さの制限」オプションを有効にし、制限を100に設定し、ドロップダウンから「文字」を選択します。

詳細設定]タブの[長さの制限]オプションを有効にします。

Limit Lengthオプションの詳細については、こちらのドキュメントをご覧ください

制限文字数の変更

さあ、スニペットをサイトに追加しましょう。

あなたのサイトにスニペットを追加する方法や場所がわからない場合は、こちらのチュートリアルをご覧ください

/**
 * Change the text for the character limit.
 *
 * @link https://wpforms.com/developers/how-to-change-the-limit-character-validation-text/
 */

function wpf_dev_frontend_strings( $strings ) {
	
    // val_limit_words when using words
    // val_limit_characters when using characters   
    
    // Change the message that will appear to your visitors after the = sign below
    $strings[ 'val_limit_characters' ] = __( 'You have used {count} characters out of allotted {limit}. You have {remaining} remaining.', 'plugin-domain' );

	return $strings;
}

add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );

このスニペットで、ユーザーは文字数制限の残り文字数を確認できる。

これで完了です!これで、文字数制限または単語制限を使用しているときにフォームフィールドの下に表示される制限文字数のテキストを変更することができました。 フォームフィールドの最小文字数も設定したいですか? チュートリアル「テキストフォームフィールドに最小文字数を設定する方法」をご覧ください。

リファレンス・フィルター

wpforms_frontend_strings

よくあるご質問

Q: 文字数制限ではなく、単語数制限を使用している場合、テキストはどのように変更できますか?

A:ワード制限のテキストを変更するには、以下のコード・スニペットを使用してください:

/**
 * Change the text for the character limit.
 *
 * @link https://wpforms.com/developers/how-to-change-the-limit-character-validation-text/
 */

function wpf_dev_frontend_strings( $strings ) {
	
    // val_limit_words when using words
    // val_limit_characters when using characters   
    
    // Change the message that will appear to your visitors after the = sign below
    $strings[ 'val_limit_words' ] = __( 'You have used {count} words out of allotted {limit}. You have {remaining} remaining.', 'plugin-domain' );
	return $strings;
}

add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );

Q: 単語と文字の両方を1つの機能で変更できますか?

A:もちろんです!このスニペットを使えば、1つの関数で単語と文字の両方を変更することができます。

/**
 * Change the text for the character and word limit.
 *
 * @link https://wpforms.com/developers/how-to-change-the-limit-character-validation-text/
 */
  
function wpf_dev_frontend_strings( $strings ) {
      
    // val_limit_words when using words
    // val_limit_characters when using characters   
      
    // Change the message that will appear to your visitors after the = sign below
    $strings[ 'val_limit_words' ] = __( 'You have used {count} words out of allotted {limit}. You have {remaining} remaining.', 'plugin-domain' );
    $strings[ 'val_limit_characters' ] = __( 'You have used {count} characters out of allotted {limit}. You have {remaining} remaining.', 'plugin-domain' );
    return $strings;
}
  
add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );

Q: 表示されているテキストを完全に削除することはできますか?

A:もちろん、このメッセージを削除することは可能です。しかし、訪問者が入力中に突然入力制限のために入力できなくなった場合、フォームに記入する際に混乱を招く可能性があることに留意してください。制限を説明する何らかのメッセージを残しておくことをお勧めします。それでも表示されるメッセージを完全に削除したい場合は、代わりにこのスニペットを使用してください。

/**
 * Remove the text for the character and word limit.
 *
 * @link https://wpforms.com/developers/how-to-change-the-limit-character-validation-text/
 */
 
function wpf_dev_frontend_strings( $strings ) {
     
    // val_limit_words when using words
    // val_limit_characters when using characters   
     
    // Change the message that will appear to your visitors after the = sign below
    $strings[ 'val_limit_words' ] = '';
    $strings[ 'val_limit_characters' ] = '';
    return $strings;
}
 
add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );