voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Removing classes and image attributes #90

Closed mttindustries closed 8 years ago

mttindustries commented 8 years ago

Is there a way to remove the default attributes wordpress seem to generate? When you view the html source it comes up with;

<img width="600" height="450" src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="hotplate2thumb" />

I want to remove the width and height because that's invalid code. And I want to remove the class too, to make the site look less wordpressy.

I've already got this in the functions.php

//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
        $output = preg_replace('/class=".*?"/', '', $output);
        return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

But that only works for wordpress' defult post thumbnails and not the multiple/secondary one.

I also tried implementing the code like so on the template:

<?php 
if (class_exists('MultiPostThumbnails')): 
echo "<img src=\"".MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image') . "\" alt=\"\" />"; 

endif; ?>

But that still renders a second image for all posts even if I don't have one set, so then I get broken images appearing in the theme. Not all my posts use a second image, only a few.

Thanks

mttindustries commented 8 years ago

I appear to have fixed the issue (finally) by adding

add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );

to the function

//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
        $output = preg_replace('/class=".*?"/', '', $output);
        return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

in the theme's functions.php file

chrisscott commented 8 years ago

You should also be able to do this more cleanly by adding a filter on wp_get_attachment_image_attributes. If you don't want it to apply to all functions that use it, you can add it conditionally and remove it so it doesn't affect other cases.