funtions.php

Posted in: Newspaper
Post count: 63

Hi.
Can I add this code to functions.php to place ALT attributes on the images? Or can you create some incompatibility ??
Thank you.

function add_alt_tags( $content ) {
preg_match_all( ‘//’, $content, $images );
if ( ! is_null( $images ) ) {
foreach ( $images[1] as $index => $value ) {
if ( preg_match( ‘/alt=””/’, $value ) ) {
$new_img = str_replace(
‘alt=””‘,
‘alt=”‘ . esc_attr( get_the_title() ) . ‘ ‘. $index . ‘”‘,
$images[0][$index] );
$content = str_replace(
$images[0][$index],
$new_img,
$content );
}
}
}
return $content;
}
add_filter( ‘the_content’, ‘add_alt_tags’, 99999 );

Post count: 20687

Hi,

You can set alt text for images inside the post content like this
https://www.screencast.com/t/TmOBi65s2
https://www.screencast.com/t/yJlT6w3EIY
If you need something else you can go ahead and test that code in the functions file, then check the results.

Thanks

Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.