WordPress - remove the width and height attributes on avatars

749 views Asked by At

Why does this filter inside my functions.php not work on the get_avatar() function inside the comments?

// Remove height/width attributes on avatar img tags.
function myscript_remove_dimensions_avatars( $avatar ) {

    $avatar = preg_replace( '/(width|height)=\"\d*\"\s/', "", $avatar );

    return $avatar;

}
add_filter( 'get_avatar', 'myscript_remove_dimensions_avatars', 10 );

.

Inside my comments template I use this PHP tag to print the (gr)avatars.

<?php echo get_avatar( $comment, 96 ); ?>
1

There are 1 answers

0
Jack Ottermans On

Found the solution it's a little tiny difference but it works. :)

It's the double vs single quotes near the \d* section.

// Remove height/width attributes on avatar img tags.
function myscript_remove_dimensions_avatars( $avatar ) {

    $avatar = preg_replace( "/(width|height)=\'\d*\'\s/", "", $avatar );

    return $avatar;

}
add_filter( 'get_avatar', 'myscript_remove_dimensions_avatars', 10 );