• Hi,
    Im trying remove all IMG classes (including Woocommerce). I have tried add_filter( 'get_image_tag_class', '__return_empty_string' ); but it doesnt work. Is there other solution?
    Thanks you

Viewing 6 replies - 16 through 21 (of 21 total)
  • Thread Starter gore.m

    (@gorem)

    Thanks you, Ive tried suggested function, but it doesnt work again.

    Here is result / full html:

    <img width="433" height="433" src="http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-433x433.png" class="attachment-medium size-medium wp-post-image" alt="" srcset="http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-433x433.png 433w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-150x150.png 150w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-768x768.png 768w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-640x640.png 640w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-153x153.png 153w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-498x498.png 498w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-624x624.png 624w, http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462.png 960w" sizes="(max-width: 433px) 100vw, 433px" id="exifviewer-img-6" exifid="-379746270" oldsrc="http://localhost/wp-content/uploads/2017/03/Facebook-960x960-grid_0005_DSC_1462-433x433.png">

    By “main” I mean “mine” function, Im sorry about my English .-)

    I think Im not doing anything exceptional. I upload images by media upload and than I add it as post feature image, result is "attachment-medium size-medium wp-post-image".
    Classes you are pointing out Ive got on page where I used your suggested code in page template (there was alingleft class), because classes "attachment-medium size-medium wp-post-image" were gone, I think it have to be an problem with image inserting.

    Moderator bcworkz

    (@bcworkz)

    Arrrgh! Well, color me stupid. (idiomatic phrase, it doesn’t make literal sense) The preg_replace() doesn’t work quite the way I expected. Serves me right for not testing. This has been tested to work correctly:

    function strip_entire_img_class($html) {
        return preg_replace('/(<img .*class=")(.+?)"/', '$1"', $html);
    }
    add_filter('the_content', 'strip_entire_img_class');

    Even though the original code “worked”, it didn’t work the way I expected for the same reason. This is better:

    function strip_entire_image_class($html) {
      return preg_replace('/( class=")(.+?)"/', '$1"', $html);
    }
    add_filter('get_image_tag', 'strip_entire_image_class', 999 );

    Also tested to work, this version retains the class attribute but strips out any values. While it works for me, it maybe still does not work for yours for unknown reasons? But you say a very similar version that strips the entire class attribute works? The one referred to as main or mine? If so, then the above will work just as well but retain the class attribute with no values. Use whichever you prefer. IMO it’s better to provide the attribute with no values than to remove it entirely. It’s less likely to break other code that may be introduced at some point.

    If the main/mine function works, why not use it and be done? Why strip classes on output? What am I missing?

    About the main/mine confusion — no worries, misunderstanding will occur even when the correct words are used. Overall your English is quite good! Inappropriate similar sounding word substitutions (“homophones”) can happen even when native speakers write.

    Thread Starter gore.m

    (@gorem)

    Thanks you.
    Well, any function that we tried doesnt work completely.

    Your last function

    function strip_entire_img_class($html) {
        return preg_replace('/(<img .*class=")(.+?)"/', '$1"', $html);
    }
    add_filter('the_content', 'strip_entire_img_class');

    Strips classes only if image is inserted by yours way echo get_image_tag( 1104, 'test image', // etc.... or if I upload image and than clink on its own permalink – there are no classes.

    But any of functions we tried doesnt work, if image is inserted as featured image for post.

    Moderator bcworkz

    (@bcworkz)

    Yes, every function that outputs an image would use its own filter, so additional hooks are required for each image function. In most cases, the same callback can be used. For featured images, adding this line to the other strip entire code should take care of them.
    add_filter('post_thumbnail_html', 'strip_entire_img_class');

    Thread Starter gore.m

    (@gorem)

    WOW! It is working .-) Thanks you .-)

    We already discussed it, but Im not sure I understood well,… Im curious where WP is taking names of those classes "attachment-medium size-medium wp-post-image". Are they WP standards? Ive noticed, that they change if I switch to other theme.

    Moderator bcworkz

    (@bcworkz)

    You’re most welcome!

    WP initially assigns some default classes, but themes and plugins can use filters to add, remove, or alter these, much like how we used filters to remove the attribute altogether. The WP defaults are something like align-left size-medium wp-image-1234

    I’m sorry it took so long to figure out the right filters and about my borked preg_replace regexp. I’ve no excuse, coding is like that sometimes. Persistence pays off though, keep trying until a solution is found 🙂

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘How remove IMG classes?’ is closed to new replies.