Vous souhaitez modifier la limite de caractères affichée sous vos champs de formulaire, en particulier lorsque vous utilisez la limite de caractères pour les champs de formulaire Texte à ligne unique ou Texte à paragraphe?
Par défaut, vous verrez un message sous le champ du formulaire indiquant le nombre actuel de caractères par rapport à la limite maximale, par exemple 0 sur 50 caractères maximum ou 0 sur 50 mots maximum, selon les paramètres de votre formulaire. Vous pouvez personnaliser ce message pour y inclure le nombre de caractères restants. Cet ajustement est facilement réalisable à l'aide d'un simple extrait de code PHP. Dans ce tutoriel, nous verrons comment utiliser PHP pour modifier ce message.
Création du formulaire
Tout d'abord, nous devons créer notre formulaire et ajouter nos champs de formulaire.
Si vous avez besoin d'aide pour créer votre formulaire, consultez cette documentation.
Nous avons ajouté un champ de formulaire Texte de paragraphe pour contenir notre message gravé.
Fixer la limite de caractères
Ensuite, sélectionnez le texte du paragraphe que vous avez ajouté et cliquez sur l'onglet Avancé pour activer l'option Limiter la longueur, fixer la limite à 100 et sélectionner Caractère dans la liste déroulante.
Pour plus d'informations sur l'option Limiter la longueur, veuillez consulter cette documentation.
Modification du texte de validation du caractère limite
Il est maintenant temps d'ajouter l'extrait à votre site.
Si vous ne savez pas comment ou où ajouter des snippets à votre site, consultez ce tutoriel.
/** * 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 );
Et c'est tout ! Vous avez réussi à modifier le texte de la limite de caractères qui s'affiche sous le champ du formulaire lorsque vous utilisez la limite de caractères ou de mots. Souhaitez-vous également définir un nombre minimum de caractères pour votre champ de formulaire ? Consultez notre tutoriel sur la définition d'un nombre minimum de caractères pour un champ de formulaire texte.
Filtre de référence
FAQ
Q : Comment puis-je modifier le texte lorsque j'utilise la limite de mots et non la limite de caractères ?
R : Pour modifier le texte de la limite de mots, veuillez utiliser l'extrait de code suivant :
/** * 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 : Puis-je modifier ce paramètre pour les mots et les caractères en une seule fonction ?
R : Absolument ! Vous pouvez modifier ce texte à la fois pour les mots et les caractères dans une seule fonction en utilisant cet extrait.
/** * 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 : Puis-je supprimer complètement le texte qui s'affiche ?
R : Vous pouvez, bien entendu, supprimer ce message. Mais n'oubliez pas que si vos visiteurs sont en train de taper et qu'ils ne peuvent soudainement plus taper en raison de la limite, cela peut créer une certaine confusion lorsqu'ils remplissent le formulaire. Nous vous recommandons de laisser un message expliquant la limite. Toutefois, si vous souhaitez toujours supprimer le message qui s'affiche complètement, utilisez cet extrait à la place.
/** * 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 );