0

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.

4
1

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.

1
  • 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
0

Solution:

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;
}
1
  • great to hear that – janw Jul 13 '12 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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