WordPress.org

Forums

[resolved] How to use wp_post_thumbnail_class_filter (3 posts)

  1. humanitas
    Member
    Posted 4 years ago #

    I would like to remove the class attribute from the post image thumbnail. It currently gets printed as:

    class=" wp-post-image"

    I would like to eradicate it entirely! Or, if not possible, then to replace it with:

    class=""

    I have studied the filter int he source code and tried to use it as set out below. However, it fails to work. Unfortunately there is no reference to the use of this filter on these forums or on Google. Is it possible to suggest an amendment or correction to my attempt?
    Many thanks.

    `function my_wp_post_thumbnail_class_filter_remove( $class ) {
    $attr['class'] .= '';
    return $attr;

    add_filter( 'wp_get_attachment_image_attributes', 'my_wp_post_thumbnail_class_filter' );
    }

  2. keesiemeijer
    moderator
    Posted 4 years ago #

    try it with this in your theme's functions.php:

    add_filter( 'post_thumbnail_html', 'remove_thumbnail_class', 10 );
    function remove_thumbnail_class( $html ) {
    	$html = preg_replace( '/class="(.*?)"/', '', $html );
        return $html;
    }
  3. humanitas
    Member
    Posted 4 years ago #

    Many thanks! That works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic