Introduction
Would you like to change the limit character text that appears under the form field when using the Character Limit on Single Line Text or Paragraph Text form fields? By default, you will see a message under the form field that will state 0 out of 50 max characters or 0 out of 50 max words (depending on your form settings). You can easily change this text with a small PHP snippet. In this tutorial, we’ll give you the PHP needed to change this text.
Creating the form
First, we’ll need to create our form and add our form fields.
If you need any help in creating your form, please check out this documentation.
We’ve added a Paragraph Text form field to hold our engraved message.
Setting the character limit
Next, select the Paragraph Text you added and click on the Advanced tab to enable the Limit Length option and set the limit to 100 and select Character from the dropdown.
For further information on the Limit Length option, please review this documentation.
Adding the snippet
Now it’s time to add the snippet to your site.
If you’re not sure how or where to add snippets to your site, please check out this tutorial.
/** * 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}.', 'plugin-domain' ); return $strings; } add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );
And that’s it! You’ve successfully changed the limit character text that shows under the form field when using the character or word limit. Would you also like to also have a minimum number of characters set for your form field? Take a look at our tutorial on How to Set a Minimum Number of Characters on a Text Form Field.
Related
Filter Reference: wpforms_frontend_strings
FAQ
Q: How would I change the text when using Word limit and not Character limit?
A: To change the text for the Word limit, please use the following code snippet:
/** * 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}.', 'plugin-domain' ); return $strings; } add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );
Q: Can I change this for both words and characters in one function?
A: Absolutely! You can change this text for both words and characters in one single function by using this snippet.
/** * 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}.', 'plugin-domain' ); $strings[ 'val_limit_characters' ] = __( 'You have used {count} characters out of allotted {limit}.', 'plugin-domain' ); return $strings; } add_filter( 'wpforms_frontend_strings' , 'wpf_dev_frontend_strings', 10, 1 );
Q: Can I remove the text that shows completely?
A: You can, of course, remove this message. But keep in mind, if you’re visitors are typing and then suddenly can’t type anymore due to the limit, this could cause confusion when completing the form. We recommend leaving some sort of message explaining the limit. However, if you’d still like to remove the message that appears completely, use this snippet instead.
/**
* 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 );
[/php]