Select Page

Today, I’ll show you how to remove CSS classes from Post Thumbnails in WordPress posts. It’s really simple and only requires adding code to your WordPress website.

Place this code in your theme’s functions.php file or create a custom plugin with this code:

/**
 * Custom function to remove classes from post thumbnails
 * @param array $attr An array of post thumbnail attributes
 * @return array $attr The modified array of post thumbnail attributes
 */
function remove_classes_from_post_thumbnails( $attr ) {
  // Remove the 'wp-post-image' class from the post thumbnail
  $attr['class'] = str_replace( 'wp-post-image', '', $attr['class'] );
  return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'remove_classes_from_post_thumbnails', 10, 1 );

This code removes the ‘wp-post-image’ class from post thumbnails in WordPress. The ‘wp_get_attachment_image_attributes’ filter is a WordPress hook that allows you to modify the attributes of the image element used to display post thumbnails. By default, the ‘wp_get_attachment_image_attributes’ filter adds the ‘wp-post-image’ class to the ‘class’ attribute of the image element.

The ‘remove_classes_from_post_thumbnails’ function is a custom function that takes one argument: $attr. The $attr argument is an array of post thumbnail attributes.

Inside the function, the ‘str_replace’ function replaces the ‘wp-post-image’ class in the ‘class’ attribute with an empty string. This removes the ‘wp-post-image’ class from the post thumbnail. Finally, the function is hooked to the ‘wp_get_attachment_image_attributes’ filter using the ‘add_filter’ function, which applies the changes to the post thumbnail attributes.

This code will remove the ‘wp-post-image’ class from post thumbnails on your WordPress site. You can modify the code to remove a different class if desired.