Improve accessibility of a WordPress site: images and tables
Web accessibility is a great idea, especially when your site targets a wide audience. In this article we will look at only a small part of it on a WordPress site: images and tables.
Problem: Make images and tables on a WordPress site more accessible for screen readers.
Solution: Mark decorative images so screen readers skip them, and add header cells with scope to tables in the content.
Images
An image that carries information needs a meaningful alt, set in the Media Library. A decorative image (an icon, a divider, a background picture) needs an empty alt, and role="presentation" makes it clear for assistive technology that it can be skipped.
If images are printed with wp_get_attachment_image(), the attribute can be added in a filter. Add it only when the alt is empty: on an image with a real alt, role="presentation" would hide it from screen readers.
<?php
add_filter( 'wp_get_attachment_image_attributes', 'add_presentation_role_to_image' );
function add_presentation_role_to_image( $attr ) {
if ( empty( $attr['alt'] ) ) {
$attr['alt'] = '';
$attr['role'] = 'presentation';
}
return $attr;
}
Tables
Screen readers use scope="col" and scope="row" to connect data cells with their headers. For tables in the content the solution is less elegant, a filter on the_content:
<?php
add_filter( 'the_content', 'table_accessibility_filter' );
function table_accessibility_filter( $content ) {
if ( 'post' !== get_post_type() && 'page.php' !== basename( get_page_template() ) ) {
return $content;
}
// Column headers
$content = str_replace( '</th><th>', '</th><th scope="col">', $content );
// The first cell of each row becomes a row header
$content = preg_replace( '/<tr><td>(.*?)<\/td>/', '<tr><th scope="row">$1</th>', $content );
return $content;
}
As you can see, the attributes are added only for posts and for the default page template. The second replacement assumes that the first column of every table is a header. If your tables are different, remove that line.
Bonus: responsive tables
The same filter can wrap tables for horizontal scrolling on mobile and add the Bootstrap table class:
<?php
add_filter( 'the_content', 'table_content_filter' );
function table_content_filter( $content ) {
if ( 'post' !== get_post_type() && 'page.php' !== basename( get_page_template() ) ) {
return $content;
}
// Wrapper and Bootstrap class for responsive tables
$content = str_replace( '<table', '<div class="table-responsive"><table', $content );
$content = str_replace( '</table>', '</table></div>', $content );
$content = str_replace( '<table class="', '<table class="table ', $content );
// Remove extra spaces in class names (W3C validator)
$content = preg_replace( '/class=["\']\s*(?P<class>[^"\'<>]+?)\s*["\']/i', 'class="$1"', $content );
// Accessibility
$content = str_replace( '</th><th>', '</th><th scope="col">', $content );
$content = preg_replace( '/<tr><td>(.*?)<\/td>/', '<tr><th scope="row">$1</th>', $content );
return $content;
}
Use this one instead of the previous filter, not together with it, otherwise the scope attributes are added twice.