WordPress Filter Examples – Changing Attributes When Adding Images to Posts
June 5th, 2010 by
Typically, when you add an image to your posts, wordpress assigns certain attributes to the img tag, depending on what you chose in the image upload editor form.
The following selection set…

Will result in the following image tag added to your post’s HTML code:
<img src="http://www.webtechwise.com/wp-content/uploads/2010/06/rss-icon.jpg" alt="" title="rss-icon" width="30" height="43" class="aligncenter size-full wp-image-1525" />
However, in some cases you may want to edit these attributes to suit your needs.
Like in many other WordPress tweaks, the add_filter function comes to play, so this is a great way to learn how to use filters and what do apply_filters and add_filter mean exactly.
The get_image_tag function (below) is included in WordPress core files (specifically, in the wp-includes/media.php file).
This function creates the img tag for the image you inserted. As you can see, it handles some important attributes for your image, including ID, title, alt, align and size.
function get_image_tag($id, $alt, $title, $align, $size='medium') {
list( $img_src, $width, $height ) = image_downsize($id, $size);
$hwstring = image_hwstring($width, $height);
$class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id;
$class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />';
$html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
return $html;
}
Line 5 above (highlighted), includes the apply_filters call, which is called a hook, or in other words, an invitation for you to rewrite the function specified in the hook (in this case, set a new class for the image).
As I explained in this post, it is not wise to edit WordPress core files directly.
Instead, you should create a new filter function in the functions.php file located in your WordPress theme folder.
For the example, let’s set the class of the image to always be “post_image”:
Add the following filter function to functions.php:
function my_image_tag_class($class){
$class='post_image';
return $class;
}
add_filter('get_image_tag_class','my_image_tag_class');
If you take another look at the get_image_tag function, you will see that there is one more apply_filters hook in line 7 – $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
This hook, invites you to rewrite the entire get_image_tag function with all the image attributes and not just its class.
For the example, let’s set the alt attribute to always have the same value as the title attribute.
To rewrite the entire get_image_tag function, add the following filter function to functions.php:
function my_image_tag($html, $id , $alt, $title){
$html = str_replace('alt=""','alt="'.$title.'"',$html);
return $html;
}
add_filter('get_image_tag','my_image_tag',10,4);
Note that in line 5 (highlighted above), the add_filter call now contains 2 more parameters that did not exist in the previous add_filter call. The first parameter is the priority of the filter, and the second parameter is the number of acceptable arguments in the function. I left the priority to default (10) and I set the acceptable args to 4, because I need to pass the $title attribute’s value to the function.
Got any questions or comments? Shoot!




Nice tip. Your code can replace the SEO Image plugin. That’s very good: simpler solution for a good result
Thanks man. I’m glad you like it
Hi,
Could you explain more about rewriting the 2 functions my_image_tag_class and my_image_tag? Do I need to edit both of them and where should I call them?
Thanks.
Hi Tek3D. These 2 filters do not depend on each other so you don’t have to use both, unless you want to both edit the image class attribute and other attributes of the image.
my_image_tag_class is specific for the image class, while my_image_tag enable editing all image attributes.
You should place the filter functions in your functions.php theme file
Thanks Omer,
I want to ask 1 more question. Where would I define the “post-image” class? I am not a web developer so I couldn’t understand this.
Take a look at line 2 at the second code block. It states $class=’post_image’;
Wow Great tip.Surely I’ll apply in my blog.Thanks for sharing.
Hi Omer, this is way over my head, so I need to start with a basic question, why would I want to change this attribute and how will it help me? Can you give me an example of an image with the filter vs. without the filter? Thanks for your patience with me!
Hi ileane! always nice to see you here.
The example in this post shows you how you can change the default attributes when adding an image in the admin.
If you add an image when in HTML view, you will notice that it automatically adds class=”alignnone size-large wp-image-2649″ to your image. I explain here how you can change that default WordPress behavior by using Filters.
Ok, got it now. I never add images in HTML view (but on Blogger I need to embed YouTube videos in HTML view) and it seems I change the attributes each time I add something. HA! Thanks for explaining!
Great tip and easy to follow. I have it working on a site i’m building. Is it possible to have all the images within a post linked to a particular website using this function.
I’d also like to change this website for each post I make, without creating a new link for each picture.
Good question Mose, you can use the filter in the get_image_send_to_editor function.
This function is located in the wp-admin/includes/media.php.
The parameter you are looking for is $url.
Is it possible to set the ‘rel’ tag of a newly posted image using this method? I am using a lightbox script which needs ‘gallery’ appended to the ‘rel’ tag to link multiple images in a gallery.
I’ve had a look through the ‘media.php’ file but I couldnt see anything which might do this.
Rich,
You can use the same filter I use in the above example – $html = apply_filters( ‘get_image_tag’, $html, $id, $alt, $title, $align, $size );
only instead of these attributes, you can pass a rel attribute..
this is useful. It works well for me except i was looking to add a class to the a-tag the image tag in contained in. any ideas?
Hi Frank, would you like this class attribute to be the same for each time you add an image?
hi Omer thanks for the quick response. yes i do. just to clarify instead of having the class added to the img tag i would like it to be added to the a tag the image is contained in.
so with your code I would get (using class=”special”)..
but I would like to have
Frank, you need to somehow overwrite the function get_image_send_to_editor. This function doesn’t seem to have a call for apply_filter that relates to the attributes of the a tag, so I’m not sure how.
Perhaps copying this function to your function.php file and add that specific class you wanted would do the trick.
Hi Omer! Your description is great, but i looking for something more. I would like to insert the IMG tag into a SPAN, to reach the needs of our design.
Could you help me, how to start?
Hugo, thanks. Before I answer your question, do you need a span tag around each and every image you add to your posts or just in particular cases?
Yes Omar. I need a SPAN tag around every image that came across posts, through the_content().
Finally i modified your code to this:
function span_my_image_tag($html, $id , $alt, $title){
$html = str_replace('alt=""','alt="'.$title.'"',$html);
$html = str_replace('<img','',' />',$html);
return $html;
}
add_filter('get_image_tag','span_my_image_tag',10,4);
but at first sight i hope i could find a better and nice solution like remove_filter(‘wp_get_attachment_link’) .. and apply a new filter to do what i want, but it doesnt works :S
function span_my_image_tag($html, $id , $alt, $title){
$html = str_replace('alt=""','alt="'.$title.'"',$html);
$html = str_replace('<img','< span class="imgcanvas" >',' />< /span >',$html);
sorry, i need to replace to <-> because the editor strips out the span tag...
return $html;
}
add_filter('get_image_tag','span_my_image_tag',10,4);
Hugo, your solution seems fine to me..doesn’t it work?
It works Omar
– I’ll try to catch a better solution with use of remove_filter(), apply_filter() on wp_get_attachment_link. I think that should be better and cleaner than the replace above.
Do you have any ideas, how to do this with filters?
I didn’t have a chance to delve into the wp_get_attachment_link function just yet but I assume it contains a hook to which you can assign your custom filter.
I’d like to do something similar to what both Rich and Hugo are looking for… I have a function which uses wp_get_attachment_link to display thumbnails of each attached photo, and each thumbnail is wrapped in a link to the actual full-sized image file. I’d like to add a rel attribute to each link – the same rel for each image attached to the same post, and a unique rel between the different posts – so that my jquery lightbox will automatically group the images into a separate ‘gallery’ for each post. I figure the thing that makes the most sense is to insert each post’s ID as the rel attribute.
So, for each attachment link, I need to get that attachment’s parent post ID and insert it as a rel attribute… does this make sense?
I’ve tried several ways of doing this by hooking into apply_filters in wp_get_attachment)_link, but I must be doing something wrong… keep getting parsing errors.
Any ideas on how to achieve this?
I understand your need, Jerermy, unfortunately I wasn’t able to figure out how to manipulate that a tag that wraps images yet. If you find out in the future, please share! thanks
I think I did get it figured out – though not without a lot of trial and error. You can find the code here: http://jeremybuller.com/files/my_get_attachment_image.txt (didn’t want the comments thing to garble the code)…
As you can see, it’s very similar to what you posted. Just ran into a couple of snags along the way…
Fantastic post, extremely useful! Just one question though – is there a way to make this function conditional? I.e. something along the lines of -
if (has_tag(‘my tag’)…
to apply a specific img class only to images within posts with a particular tag? I’ve dabbled with the above, but to no avail. I should point out that I’m very inexperienced, so may have put the code in the wrong place etc.
I would be very grateful for any help and again many thanks for sharing your knowledge in the first place!
Thanks Daniel. This and this posts should be able to help. The idea explained is how to get the tags of a post outside the loop. Tell me if it works for you.
Thanks Omer! I’ll let you know if I can get it to work
Your function looks pretty good, Jeremy. I haven’t tested it but if it works for you then congrats!
Do you know of a way I could do this for the post’s text? I run a blog with multiple authors, and I spend a ton of time every day fixing the same HTML codes (removing inline style tags and the divs that wordpress inserts). I would love to create a filter to automatically replace html within posts with my own rules. Would this be possible?
Hi Ashley. What you need to do is write a filter for the content that will remove inline style attributes and will look like this:
<?php
function filtered_content($content) {
$content=preg_replace(‘#(<[a-z ]*)(style=("|\’)(.*?)("|\’))([a-z ]*>)#’, ‘\\1\\6′, $content);
return $content;
}
add_filter ( ‘hook_name’, ‘your_filter’);
?>
To remove other attributes you can replace the preg_replace line with this one
$content = preg_replace(‘/(bgcolor|align|valign|size)=”[^"]*”/i’,”,$content);