Title: How remove IMG classes?
Last modified: May 28, 2017

---

# How remove IMG classes?

 *  [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/)
 * 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 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/how-remove-img-classes/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/how-remove-img-classes/page/2/?output_format=md)

 *  [Niels Lange](https://wordpress.org/support/users/nielslange/)
 * (@nielslange)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9175638)
 * Hello [@gorem](https://wordpress.org/support/users/gorem/),
 * The filter `add_filter( 'get_image_tag_class', '__return_empty_string' );` only
   removes the classes when you add new images. It did not remove classes of existing
   images.
 * Please note that the filter you’re using only empties the classes, but will keep
   the class element. Thus your images will look as follow:
 * `<img src="https://mydomain.com/wp-content/uploads/myimage.png" class="" alt=""
   width="150" height="150" />`
 * If you want to remove the entire class element, please use the following code
   snippet:
 *     ```
       add_filter('get_image_tag', 'strip_entire_image_class');
       function strip_entire_image_class($html) {
         return preg_replace('/ class="(.*)"/', '', $html);
       }
       ```
   
 * Using this snippet let’s your images look like this:
 * `<img src="https://mydomain.com/wp-content/uploads/myimage.png" alt="" width="
   150" height="150" />`
 * Have I mentioned that I would not recommend to remove the classes in first place?
   I assume you have a good reason to remove them in your case.
 * Best,
    Niels
    -  This reply was modified 9 years ago by [Niels Lange](https://wordpress.org/support/users/nielslange/).
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9175762)
 * Hello Niels,
    thanks you. No, you didn’t mentioned why you wouldn’t recommend
   it 🙂 Can you do it please? Im not using img classes, so I thought it is good
   idea to strip them out.
 * Snippet doens’t work:
 *     ```
       add_filter('get_image_tag', 'strip_entire_image_class');
       function strip_entire_image_class($html) {
         return preg_replace('/ class="(.*)"/', '', $html);
       }
       ```
   
    -  This reply was modified 9 years ago by [gore.m](https://wordpress.org/support/users/gorem/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9181899)
 * Works for me. It will typically only work for newly inserted images. Once inserted,
   the classes become part of the content and you’d need to edit the content to 
   remove classes. Also, if any function besides get_image_tag() is used, this code
   would have no effect.
 * I agree with Niels, this is not a good idea. Even if you are not using classes
   now, they do no harm other than a very small increase in data transmitted. If
   you should change themes in the future, your new theme may make use of these 
   classes and posts with missing classes will not display correctly. It’s a lot
   harder to restore missing classes than it is to remove them.
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9189700)
 * Thanks you.
    Strange… it doesnt work for me. I dont have any get_image_tag() 
   in function.php or in templates. Classes output even if images are newly inserted.
   🙁
 * According to your suggestion, what about add my own class and filter default 
   classes out?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9192105)
 * Just substitute your own class for the empty replacement in the preg_replace()
   function. get_image_tag() is a core function used when inserting images through
   the media library on a post edit screen. It doesn’t matter what theme you have
   or what’s in functions.php or templates. The substitution is done on insertion,
   not on output. If images are being placed by some other mechanism, this code 
   will not work. In that case the solution would depend on how the images are being
   placed.
 * It’s also possible your theme or one of your plugins is also somehow manipulating
   the same filter so that this code has no effect. You could try hooking late so
   your function has the final say.
 *     ```
       add_filter('get_image_tag', 'strip_entire_image_class', 999 );
       // same function declaration
       ```
   
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9193164)
 * Thanks you.
 * Still cant get it work :-/
    So for to be clear, Ive tried:
 * – I disabled “Compress JPEG & PNG images” plugin, that was only one plugin for
   images.
    – Ive tried
 * `add_filter( 'get_image_tag_class', '__return_empty_string', 9999 );`
 * or
 *     ```
       function strip_entire_image_class($html) {
           return preg_replace('/ class="(.*)"/', '', $html);
       }
       add_filter('get_image_tag', 'strip_entire_image_class', 999);
       ```
   
 * than
 * – I inserted image with Media > Upload > Chose > edit > Click on link above image/
   or Insert image to post > Publish > Preview
 * Nothing happened.
    Still have classes `attachment-medium size-medium` or `attachment-
   large size-large wp-post-image` or `attachment-shop_single size-shop_single wp-
   post-image`.
 * Im using _s theme, Ive tried Twenty Eleven with same output.
    -  This reply was modified 9 years ago by [stephencottontail](https://wordpress.org/support/users/stephencottontail/).
    -  This reply was modified 9 years ago by [gore.m](https://wordpress.org/support/users/gorem/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9195921)
 * “attachment-**shop**_single”
 * Shop? Are you using WooCommerce? Adding images through Product Image or Product
   Gallery? That would qualify as “images are being placed by some other mechanism”.
   The get_image_tag() is used for images added to post content. WC product images
   are different. Unfortunately, I’m unsure how product images are handled. I suggest
   you inquire in the [WC dedicated support forum](https://wordpress.org/support/plugin/woocommerce)
   where the WC community would have a better idea of how to remove classes from
   product images.
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9204241)
 * OK I disabled Woocommerce and used
 *     ```
       add_filter('get_image_tag', 'strip_entire_image_class');
       function strip_entire_image_class($html) {
         return preg_replace('/ class="(.*)"/', '', $html);
       }
       ```
   
 * Still It is not working `attachment-large size-large wp-post-image`.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9207000)
 * There could still be influence from your theme or other plugins. Are the images
   you are using inserted into post content by using the media library? For example,
   images added as the post’s featured image is not considered as being inserted
   into post content. Only images added into the tinyMCE editor is part of post 
   content.
 * Here’s a test to try. Determine the attachment ID of an image. Any image in the
   media library that has a ‘large’ size will work. On a template somewhere (a page
   or single post template would be good choices), place the following code, then
   check the HTML output for class attributes:
 *     ```
       // replace ATCH_ID below with an actual attachment ID
       echo get_image_tag( ATCH_ID, 'test image', 'Test', 'left', 'large' );
       ```
   
 * This will confirm if the filter code is working or not. If not, your theme or
   another plugin is also filtering the same function. You can try adding `999` 
   to the add_filter() line like I suggested a few posts back to see if you can 
   supersede any other filtering.
 * If this test does remove classes, then the issue is your images are being added
   in a way that bypasses this function. You’ll need to identify how that is being
   done in order for a solution to be devised.
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9208217)
 * Thanks you
    `Warning: call_user_func_array() expects parameter 1 to be a valid
   callback, function 'strip_entire_image_class' not found or invalid function name
   in C:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 300` ID is 1104,
   so I tried 1104 and ‘1104’ … is it right?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9209957)
 * Should be an integer, though PHP usually automatically type casts numeric strings
   to integers, so either should work.
    `echo get_image_tag( 1104, 'test image',//
   etc....`
 * Where did you declare the ‘strip_entire_image_class’ function? It’s apparently
   not available to templates, which may explain why it’s not working for you. The
   proper places to put a function declaration would be either a child theme’s (
   or main theme’s if you must) functions.php or the primary page of a plugin (or
   an always included file of either one).
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9210096)
 * I used what you recommended before:
 * > You can try adding 999 to the add_filter() line like I suggested a few posts
   > back to see if you can supersede any other filtering.
 * So I used (in function.php)
 *     ```
       add_filter('get_image_tag', 'strip_entire_image_class', 999 );
       // same function declaration
       ```
   
 * I removed it and used `echo get_image_tag( 1104, 'test image', 'Test', 'left','
   large' );` than I got classes:
 * `alignleft size-large wp-image-1104`
 * than I tried main:
 *     ```
       function strip_entire_image_class($html) {
           return preg_replace('/ class="(.*)"/', '', $html);
       }
       add_filter('get_image_tag', 'strip_entire_image_class', 999);
       ```
   
 * And it is working!.. no classes 🙂
 * So is it plugin or theme, how would you search for origin?
 * thanks you
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9213351)
 * How to search for origin of conflict? Disable all plugins. If the classes are
   gone, it was a plugin. If still a problem, it is the theme. You could then activate
   each plugin one at a time until the classes reappear. The last activated plugin
   is the cause. To remove a filter added by theme or plugin, you need to know the
   add_filter() parameters used. You could grep for “get_image_tag” or var_dump 
   the global $wp_filter and deduce the parameters from where the callback is located
   in the array structure.
 * That said, I don’t think the problem was a conflicting filter. Since moving the
   declaration solved the problem without removing any other filter, I have to think
   the original location was the wrong place to begin with. You said you moved it
   from _function.php_. If accurate, that is the wrong file. The proper location
   in a theme folder is _function**s**.php_ (plural with an ‘s’) I suspect that 
   was a typo and you meant _functions.php_. In that case, the only explanation 
   is you chose the functions.php of the wrong theme, or the code was placed in 
   the wrong place within that file, such as within a class or function declaration.
 * Whatever it was, it doesn’t matter much now. You got it working! Awesome! 🙂
 *  Thread Starter [gore.m](https://wordpress.org/support/users/gorem/)
 * (@gorem)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/#post-9220325)
 * Thanks you.
    Yes, it was typo, everytime Im using functions.php.
 * I did test… so it is not an plugin, I disabled all and featured image still has
   same classes, without any change, so it has to be theme.
    Im using _s Underscore,
   I haven’t been doing any changes regarding to images… I was looking at functions.
   php there is only support `add_theme_support( 'post-thumbnails' );` and in content.
   php template is `<?php the_post_thumbnail('large');?>`.. thats all I found… So,
   instead of looking for disabling should I use an function to adjust output?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/how-remove-img-classes/page/2/#post-9222225)
 * Adjusting output, so even though it is in post content, strip out classes on 
   output? That’s possible. The filter “the_content” could be used. In fact, almost
   the same callback can be used. The regexp needs to be adjusted so it only strips
   classes of img tags and not all classes. Untested, but something like this (added
   to functions.php):
 *     ```
       function strip_entire_img_class($html) {
           return preg_replace('/<img .*class="(.*)"/', '', $html);
       }
       add_filter('the_content', 'strip_entire_img_class');
       ```
   
 * “the_content” would be an unfortunate solution because it is _much_ less efficient.
 * The classes that will not go away are within double quotes, right?
    `class="alignleft
   size-large wp-image-1104"` Single quotes are valid HTML, but will not get picked
   up by the regexp we’ve been using. A long shot. I’m 99% certain they’re double,
   but a 1% chance is worth confirming at this point.
 * The previous code is not working? But it works on main? What do you mean by “
   main”? It feels like we’re missing something simple if it’s working there but
   not otherwise. From what I know of Underscore (very little), it’s unlikely to
   cause conflicts. The previous code works on my site, which is virtually the default
   WP installation, so it ought to work on yours with Underscore and no plugins 
   IF we are going about images in the same way. There’s a disconnect somewhere,
   but I can’t imagine what it might be. Maybe if I understood “main” better, I’ll
   get an idea.

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/how-remove-img-classes/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/how-remove-img-classes/page/2/?output_format=md)

The topic ‘How remove IMG classes?’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 23 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 11 months ago](https://wordpress.org/support/topic/how-remove-img-classes/page/2/#post-9253054)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
