Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm attempting to remove the title attribute for post thumbnails (on certain posts), and I assume the get_image_tag filter is the way to do this. However, what I've got so far isn't working. What do I need to change to make this work? My code:

add_filter('get_image_tag', 'image_no_title');
function image_no_title($title) {
    $title = '';
    return $title;
}

And the relevant function (from wp-includes/media.php):

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;
}
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Thanks to Rarst pointing out that I was in the wrong place, I did some more digging and ended up with this:

add_filter('wp_get_attachment_image_attributes', 'wpse_19029_no_image_title');

function wpse_19029_no_image_title ($attr) 
{
    unset($attr['title']);
    return $attr;
}
share|improve this answer
add comment

I had the same issue where I needed to append a class to the existing post image class attribute. My code was correct, as per your example, but couldn't understand why the class wasn't updating.

What I did was to reinsert the images from media library into the posts/pages (worked for featured images and custom fields) after the action and filter were ready.

It's actually quite logical... I realized that the post is stored in the database and then extracted to generate the html for the 'front-end' of the site, but no change were being made to the database entry so I couldn't actually expect any changes to be immediately visible on the site!

share|improve this answer
add comment

Filter always affect first argument, rest are there for additional info and not even passed by default.

add_filter('get_image_tag', 'image_no_title', 10, 4);

function image_no_title( $html, $id, $alt, $title ) {

    return str_replace("title=\"{$title}\"", '', $html);
}
share|improve this answer
    
I'm not sure I completely understand what you mean. Regardless, replacing my code with what you posted doesn't eliminate the title tag on post thumbnails. –  tnorthcutt Jun 2 '11 at 13:43
    
@tnorthcutt it didn't occur for me to double check - post thumbnails do not use this function at all. You need to use post_thumbnail_html filter instead in get_the_post_thumbnail() function. –  Rarst Jun 2 '11 at 13:49
    
Good point, I didn't notice that either ;). I did some more digging and ended up with a different solution, which I'll post as an answer. –  tnorthcutt Jun 2 '11 at 14:18
add comment

Your Answer

 
discard

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.