How To Remove the Aria-Required Attribute

If you validate your WordPress theme, you find that the validator complains about some unknown attributes, particularly for the comment form fields. This tutorial tells you how to fix that, with one big caveat to start. While the solution is for Genesis themes, similar solutions likely exist for other themes.

Pre-Code Advisory Note

We know it’s good to have valid code, and some developers create their sites to meet an (X)HTML specification to the letter. However, the aria-required attribute is one of those things where it’s fine for it not to be in current specifications but still have it on your site. It’s currently generally used by assistive technology user agents, such as text-to-speech browsers, to help indicate “that user input is required on the element before a form may be submitted”. On browsers and other user agents where it’s not supported, it has no negative effect.

It’s important to note that current versions of WordPress and Genesis both come with this attribute included within the comment form for the comment area itself, and on the name and email inputs if the WordPress option is selected. This is because accessibility should always trump valid code. Valid code is pointless if your visitors can’t access the content and interact with your site successfully and for that reason, you should leave the aria-required attribute intact.

The PHP Code

If you still want to remove aria-required attribute to satisfy your validation needs, then add the following to the end of your child theme’s functions.php file, just before any closing ?> you may have.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
 
add_filter( 'genesis_comment_form_args', 'child_remove_aria_required' );
/**
* Remove aria-required attributes from comment form.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/remove-aria-required-attribute/
*
* @param array $args Comment form arguments.
*
* @return array Amended arguments
*/
function child_remove_aria_required( $args ) {
 
$args = str_replace( ' aria-required="true"', '', $args );
$args[fields] = str_replace( ' aria-required="true"', '', $args[fields] );
$args[fields] = str_replace( " aria-required='true'", '', $args[fields] ); // Genesis <= 1.3
 
return $args;
 
}
view raw functions.php This Gist brought to you by GitHub.

Speak Your Mind

*