Hi @bondimedical3,
Thank you for your feedback, it’s great to hear you enjoy the plugin!
I can see the issue you are running into and we’re making a note to find a permanent solution to prevent the backslashes from being stripped.
In the meantime, you can just use different quotes to avoid having to escape the single quotes like this:
// Add alt tag to WordPress Gravatar images
function bloggerpilot_gravatar_alt($bloggerpilotGravatar) {
if (have_comments()) {
$alt = get_comment_author();
}
else {
$alt = get_the_author_meta('display_name');
}
$bloggerpilotGravatar = str_replace("alt=''", "alt='Avatar für " . $alt . "'", $bloggerpilotGravatar);
return $bloggerpilotGravatar;
}
add_filter('get_avatar', 'bloggerpilot_gravatar_alt');
I tried the new code, but it doesn’t work and simply adds the word “alt” only. In my case it’s supposed to say alt=”Avatar for Col” but with your code it simply says alt.
That is strange, I tested the code on my end and it works OK, here’s an example: https://a.supportally.com/jOwfTH.
The only thing that I can think of that could interfere is if somehow when you copy/pasted the code from above the browser switched the quotes and instead of '
you got ‘
and the search and replace doesn’t match.
Please try this version that doesn’t rely on a string replace and should be more reliable:
add_filter(
'pre_get_avatar_data',
function ( $atts ) {
if ( empty( $atts['alt'] ) ) {
if ( have_comments() ) {
$author = get_comment_author();
} else {
$author = get_the_author_meta( 'display_name' );
}
$alt = sprintf( 'Avatar for %s', $author );
$atts['alt'] = $alt;
}
return $atts;
}
);
-
This reply was modified 8 months, 1 week ago by
Mircea Sandu. Reason: More efficient snippet
Just tested the new code, and it still has the same problem. Only the word alt is placed in the code. Can you send me a patch that I can use in the meantime which fixes the backslahes from being stripped?
Hi @bondimedical3,
We are going to improve the backslashes handling in the next release. The snippet above will add a default alt text without the need of replacing html code so that you don’t have to wait for the update.
From what you are describing could it be possible that you need to clear the cache after the snippet is activated?
Thanks!
Ive cleared the cache after the changes and the result is the same. When I use the Woody plugin it works fine.
My bad, apologies. I forgot to install your shortcode into the page. Works fine now.
No problem, you can use the auto-insert option to make it run automatically on the site.
Thank you for your help here, the backslashes issue will also be addressed in the next release.