WordPress Snippets

How To Fix Images With Missing Alt Text In WordPress

4 June 2020

How to add alt text to Gutenberg images

Preferred Way – manually add alt text to each image on page.

Quick Way – automatically assign the page title as an alt text to all images where alternative description is missing.

/**
* @snippet Fix Images With Missing Alt Text In WordPress
* @author Prosvit Design Team
* @website http://prosvit.design
*/
add_filter('render_block', 'prosvit_add_gutenberg_image_alt_text', 10, 2);
function prosvit_add_gutenberg_image_alt_text($block_content, $block)
{
if ('core/image' !== $block['blockName']) {
return $block_content;
}
$block_content = str_replace('alt=""', 'alt="' . esc_attr(get_the_title()) . '"', $block_content);
return $block_content;
}

Does this snippet (still) work?

Please let us know in the comments if everything worked as expected. We would be happy to revise the snippet if you report otherwise (please provide screenshots).

Leave a Reply

Your email address will not be published. Required fields are marked *

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert