WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use the get_image_tag() function to alter the html output of an image, like so :

add_filter('get_image_tag', 'kh_image_attachment', 10, 5);
function kh_image_attachment($html, $id, $alt, $title, $align) {
    $html = '<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="' . esc_attr($img_src) . '" id="'.esc_attr($id).'" alt="' . esc_attr($alt) . '" title="'.esc_attr($title).'" class="'.$class.'" onload=lzld(this) onerror=lzld(this) />';
    return $html;

Unfortunately, the src attribute remains empty (the class attribute as well, but I couldn't care less about that). I can't for the life of me figure out why. Can you help me out here?

Thank you.

share|improve this question
Well they will be, because you're trying to access variables that aren't set, or are outside the function scope. – TheDeadMedic Jul 13 '12 at 10:41
you shouldn't use onload and onerror en.wikipedia.org/wiki/Unobtrusive_JavaScript, stackoverflow.com/questions/1931925/… – janw Jul 13 '12 at 12:05
@TheDeadMedic I thought these were global variables – Krimo Jul 13 '12 at 12:14
@janw I know, I know... – Krimo Jul 13 '12 at 12:14

try the below:
You use a filter with more then one arg, add_filter by default will give 1 arg, you use 4 (out of 6) in my example I am given all 6 args to your function. If you want to use 4 change your function back and change the number in the add_filter.

<?php add_filter('get_image_tag', 'kh_image_attachment', 10/*prio*/, 6/*number of arguments*/);
function kh_image_attachment($html, $id, $alt, $title, $align, $size)
    //your code

It should work, questions? ask.

share|improve this answer
Ok, I'm making progress : now with your code, the alt and title attributes are showing up. However, the most crucial (the image src attribute) is still empty... – Krimo Jul 13 '12 at 12:20
up vote 0 down vote accepted


I needed to use the wp_get_attachment_image_src function.

add_filter('get_image_tag', 'kh_image_attachment', 10, 5);
function kh_image_attachment($html, $id, $alt, $title, $align) {
    $image_source = wp_get_attachment_image_src( $id, 'full' );
    $html = '<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="'.$image_source[0].'" id="kh-image-attachment-'.esc_attr($id).'" alt="' . esc_attr($alt) . '" title="'.esc_attr($title).'" onload=lzld(this) onerror=lzld(this) />';
    return $html;
share|improve this answer
great to hear that – janw Jul 13 '12 at 18:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.