wp_required_field_indicator(): string

Assigns a visual indicator for required form fields.

Return

string Indicator glyph wrapped in a span tag.

Source

function wp_required_field_indicator() {
	/* translators: Character to identify required form fields. */
	$glyph     = __( '*' );
	$indicator = '<span class="required">' . esc_html( $glyph ) . '</span>';

	/**
	 * Filters the markup for a visual indicator of required form fields.
	 *
	 * @since 6.1.0
	 *
	 * @param string $indicator Markup for the indicator element.
	 */
	return apply_filters( 'wp_required_field_indicator', $indicator );
}

Hooks

apply_filters( ‘wp_required_field_indicator’, string $indicator )

Filters the markup for a visual indicator of required form fields.

Changelog

VersionDescription
6.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    To save someone else the trouble of trying to figure out how to change asterisks (*) as the required field indicator to the word (required) for accessibility reasons, here is one which works with WordPress 6.9.4 (and probably others). It is in a span element (WP site formatting removes spans so put it around the (required) in $required_indicator so you need to add it) so you can restyle the size of the text in CSS if you like.

    /* Change the required field indicator from an asterisk to (required) */
    function wpdocs_add_custom_required_indicator() {
        $required_indicator = '(required)';
        return $required_indicator;
    }
    add_filter( 'wp_required_field_indicator', 'wpdocs_add_custom_required_indicator' );

    This also requires changing wp_required_field_message() so I will add a note to that hook as well.

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