How to remove title attribute from gallery links and images

I want to remove the title attribute from within the output of a shortcode e.g.

<a href="https://wordpress.stackexchange.com/questions/64828/url" title="bobby">
   <img src="https://wordpress.stackexchange.com/questions/64828/url" title="bobby"/>
</a>

should be:

<a href="https://wordpress.stackexchange.com/questions/64828/url" >
   <img src="https://wordpress.stackexchange.com/questions/64828/url" />
</a>

I’ve a feeling this has something to do with the wp_get_attachment_link function.

1 Answer
1

I’ve found a solution :

// Remove &lt;img&gt; title attribute in 
// http://wordpress.org/support/topic/wp_get_attachment_image_attributes-filter-not-working
function remove_img_title($atts) {
    unset($atts['title']);
    return $atts;
}
add_filter('wp_get_attachment_image_attributes','remove_img_title', 10, 4);

// remove title attribute from &lt;a&gt; title attribute in 
// modified from this post : http://oikos.org.uk/2011/09/tech-notes-using-resized-images-in-wordpress-galleries-and-lightboxes/
function ah_get_attachment_link_filter( $content ) {       

        $new_content = preg_replace('/title=\'(.*?)\"https://wordpress.stackexchange.com/", '', $content );
        return $new_content;
}
add_filter('wp_get_attachment_link', 'ah_get_attachment_link_filter', 10, 4);

Leave a Comment