Remove Attributes from WP

This handy snippet of regular expression in PHP comes in handy for modifying the output of get_the_post_thumbnail.

I used it like this:

$url_large = esc_url( get_the_post_thumbnail_url( $product_id, 'large' ) );

$attr = [
    "class"         =>  "xzoom-" . $product_id,
    "xoriginal"     =>  $url_large,
    "loading"       =>  "lazy"
];

$thumbnail = get_the_post_thumbnail( $product_id, 'thumbnail', $attr );

// Strip out srcset and sizes atttributes.

$thumbnail = preg_replace( '/(srcset|sizes)="[^"]*"/', '', $thumbnail );


echo $thumbnail;

Here is the reference.

Scroll to Top