wp_required_field_message(): string

Creates a message to explain required form fields.

Return

string Message text and glyph wrapped in a span tag.

Source

function wp_required_field_message() {
	$message = sprintf(
		'<span class="required-field-message">%s</span>',
		/* translators: %s: Asterisk symbol (*). */
		sprintf( __( 'Required fields are marked %s' ), wp_required_field_indicator() )
	);

	/**
	 * Filters the message to explain required form fields.
	 *
	 * @since 6.1.0
	 *
	 * @param string $message Message text and glyph wrapped in a `span` tag.
	 */
	return apply_filters( 'wp_required_field_message', $message );
}

Hooks

apply_filters( ‘wp_required_field_message’, string $message )

Filters the message to explain required form fields.

Changelog

VersionDescription
6.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    If you’ve changed the required indicator (wp_required_field_indicator) from an asterisk (*) to (required) for accessibility reasons, you will also want to change the required field message to remove references to the glyph since it will be self-evident, so here’s a short snippet you can add to functions.php to do it. This works with WordPress 6.9.4 (and probably others).

    /* Change the message before forms to exclude the mention of the required field since it is self-evident */
    function wpdocs_add_custom_required_message() {
        $required_message = '';
        sprintf( '', sprintf( __( '' ), wp_required_field_indicator() ) );
    
        return $required_message;
    }
    add_filter( 'wp_required_field_message', 'wpdocs_add_custom_required_message' );

    There should be a span element inside the required message quotes for $required_message plus the first argument in sprintf, but WP site’s formatting will not let me insert it because it strips out HTML presumably.

You must log in before being able to contribute a note or feedback.

Sponsor
SponsoredKunjungi sekarang
Promo