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've created a custom post type as a plugin and released it into the repository. One of the core features involves using a featured image. I've added thumbnail to $supports in register_post_type(), so the meta box shows up in the Administration Panel. I'm also hooking into after_setup_theme and calling add_theme_support( 'post-thumbnails' ), but I don't think it's taking affect.

The codex says you have to call it from the theme's functions.php file, but if that's true then it'll only work if the user's theme calls add_theme_support( 'post-thumbnails' ) (which would cover all post types. If the theme doesn't call it, or only calls it on a specific type, then it won't work.

Does anyone see a way around this problem?

share|improve this question
    
try hooking the add_theme_support function to init with a low priority (99999) –  Bainternet Jul 23 '11 at 9:41
    
Thanks for the suggestion. I tried that but it didn't work. –  Ian Dunn Jul 23 '11 at 14:53
1  
Please move your solution to a separate answer, that would be more in line with site's mechanics. Btw are you sure you are not breaking case when support is enabled for all post types (simply true) ? Your code looks like it will overwrite it with your CPT alone. –  Rarst Jul 24 '11 at 9:10
    
You're right, I fixed that and moved it to a separate answer. –  Ian Dunn Jul 24 '11 at 22:45
add comment

2 Answers 2

up vote 6 down vote accepted

There are comments in core code that this should be improved, but they are there for a while already. Basically there is no native function to add or remove part of some feature, only feature altogether.

Doing it manually would be running something like this after theme is done (late on after_setup_theme hook):

function add_thumbnails_for_cpt() {

    global $_wp_theme_features;

    if( empty($_wp_theme_features['post-thumbnails']) )
        $_wp_theme_features['post-thumbnails'] = array( array('your-cpt') );

    elseif( true === $_wp_theme_features['post-thumbnails'])
        return;

    elseif( is_array($_wp_theme_features['post-thumbnails'][0]) )
        $_wp_theme_features['post-thumbnails'][0][] = 'your-cpt';
}
share|improve this answer
    
You can use get_theme_support() instead of working directly with the global. If I do a var_dump on the global or the return of get_theme_support() they're both NULL, though. I've tried with init and after_setup_theme with a priority of 99. –  Ian Dunn Jul 23 '11 at 14:56
    
Ah, I think I got it. The theme I was testing with hadn't set anything with it, so it was supposed to be null... –  Ian Dunn Jul 23 '11 at 15:11
add comment

This is what I ended up using, which is a modified version of Rarst's answer

public function addFeaturedImageSupport()
{
    $supportedTypes = get_theme_support( 'post-thumbnails' );

    if( $supportedTypes === false )
        add_theme_support( 'post-thumbnails', array( self::POST_TYPE ) );               
    elseif( is_array( $supportedTypes ) )
    {
        $supportedTypes[0][] = self::POST_TYPE;
        add_theme_support( 'post-thumbnails', $supportedTypes[0] );
    }
}
add_action( 'after_setup_theme',    array( $this, 'addFeaturedImageSupport' ), 11 );
share|improve this answer
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.