• Resolved bondimedical3

    (@bondimedical3)


    First of all, great work on adding code snippets to the plugin as I need 1 less plugin now however, I have run into an issue.

    I am adding an ALT text to the Gravatar in WordPress by following the guide here https://bloggerpilot.com/en/gravatar-alt-tag/ but when you insert the code they recommend your plugin strips away all the backslahes and I can’t activate the snippet. I am currently using Woody snippets where it works fine but would like to move over to you guys. Below is the code, how do I fix 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');
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    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');
    Thread Starter bondimedical3

    (@bondimedical3)

    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.

    Plugin Author Mircea Sandu

    (@gripgrip)

    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 2 years, 4 months ago by Mircea Sandu. Reason: More efficient snippet
    Thread Starter bondimedical3

    (@bondimedical3)

    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?

    Plugin Author Mircea Sandu

    (@gripgrip)

    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!

    Thread Starter bondimedical3

    (@bondimedical3)

    Ive cleared the cache after the changes and the result is the same. When I use the Woody plugin it works fine.

    Thread Starter bondimedical3

    (@bondimedical3)

    My bad, apologies. I forgot to install your shortcode into the page. Works fine now.

    • This reply was modified 2 years, 4 months ago by bondimedical3.
    Plugin Author Mircea Sandu

    (@gripgrip)

    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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Backslashes being removed from code snippets’ is closed to new replies.