WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

Recommend the_title_attribute() instead of esc_attr( get_the_title() )

Open GaryJones opened this issue 6 years ago • 3 comments

Is your feature request related to a problem?

the_title_attribute() automatically strips tags from get_the_title() and escapes the echoed or returned value ready for it to be added to an attribute.

We should recommend (WordPress-Extra) that the_title_attribute() be used instead of esc_attr( get_the_title() ), perhaps with a slightly different message for when that is proceeded by an echo or <?php=.

<img src="..." alt="<?php echo esc_attr( get_the_title() ) ?>">

=>

<img src="..." alt="<?php the_title_attribute(); ?>">

GaryJones avatar Jul 25 '19 15:07 GaryJones

@GaryJones Should this be added to #1315 as one of the cases to look for ?

jrfnl avatar Jul 25 '19 15:07 jrfnl

Yes, can do.

GaryJones avatar Jul 25 '19 15:07 GaryJones

This would be very welcome, as a lot of plugins get it wrong. wp-Typography optionally injects HTML into the_title and when themes or plugins don't expect that, people get upset.

mundschenk-at avatar Aug 02 '20 15:08 mundschenk-at