Lee Willis

Post Thumbnails only for Custom Post Types

| 3 Comments

The guys over at getshopped.org are working on a pretty major revision to their WP e-Commerce plugin. Part of this is migrating “products” into custom post types, including using post thumbnails for the product images. One of the problems that has cropped up in testing was where user’s themes didn’t support post thumbnails. The first fix for was for the plugin to force “theme support” for post thumbnails by calling

add_theme_support( 'post-thumbnails' );

This worked, in that it solved the problem – all of the post-thumbnail function calls worked. However, it left a bug, and one that isn’t a software, or a techie bug – it was the worst kind – a bad user experience. Suddenly “posts” and “pages” would allow you to set featured images when authoring, or editing, but because the theme didn’t really support post thumbnails, those images would never show leaving a confused, bewildered user. So, the solution is to enable the support in general, but disable it for specific post types:

function check_thumbnail_support() {
 if (!current_theme_supports('post-thumbnails')) {
   add_theme_support( 'post-thumbnails' );
   add_action('init','remove_posttype_thumbnail_support');
 }
}
add_action('after_setup_theme','check_thumbnail_support',99);

function remove_posttype_thumbnail_support() {
 remove_post_type_support('post','thumbnail');
 remove_post_type_support('page','thumbnail');
}

This does three important things:
1. Check to see if the theme supports thumbnails already – if so, do nothing
2. If not, then turn on post thumbnail support, but also …
3. Remove support from “post” and “page”

The “gotcha” if you’re trying to come up with this yourself is that theme support is for “post-thumbnails”, but post_type_support is for the more generic “thumbnails”. Hope this comes in handy some someone.

3 Comments

  1. Are you talking about 3.8 WP-eCommerce? Does that mean there will be no more support for thumbnails in our future posts? πŸ™

    • I am talking about 3.8 WP e-Commerce.

      However – it’s not a bad thing πŸ™‚

      The problem comes when someone installs WP e-Commerce (Which does support “post thumbnails” for the products custom post type) on top of a theme which doesn’t support post thumbnails for normal posts. In this case it caused problems with WP e-Commerce. The first solution was just to forcibly register post thumbnail support for the theme – however that led to the post thumbnail box being displayed for standard posts – even though the user’s theme would never display them. The solution is the code in this post πŸ™‚

  2. Ooo, so it will be ok? πŸ™‚ Well my theme didn’t support post thumbnails and I forced it πŸ™‚ by adding that code. And a plugin “Auto post thumbnail” helped.

    Thanks for your quick reply!

Leave a Reply

Required fields are marked *.