+ Reply to Thread
Results 1 to 8 of 8

Thread: Need help enabling post thumbnails for a custom post type in my plugin

  1. #1
    JLeuze's Avatar
    JLeuze is offline Here For The Peanuts
    Join Date
    Jan 2009
    Location
    Minnesota
    Posts
    125

    Default Need help enabling post thumbnails for a custom post type in my plugin

    I recently released my first plugin, Meteor Slides, which adds a custom post type for slides.

    The slide images are managed using the Featured Image functionality. To ensure that this works with older themes, my plugin is adding support for post thumbnails like this:

    PHP Code:
    add_theme_support'post-thumbnails', array( 'slide' ) ); 
    This has worked well so far to add thumbnail support for the slides post type, without adding it to posts and pages in themes that don't support this feature.

    The above code works with themes that have no thumbnail support, and it works with themes that have it fully enabled like this:

    PHP Code:
    add_theme_support'post-thumbnails' ); 
    But I just found out that when a theme enables this feature for a specific post type or types using an array, the featured image feature is disabled in my plugin.

    Here is an example of how one theme that conflicts with my plugin adds post thumbnail support:

    PHP Code:
    add_theme_support'post-thumbnails', array( 'post' ) ); 
    It doesn't matter if the theme is specifying support for posts, pages, or another custom post type; if an array is used like this there is a conflict.

    I had thought that the two arrays were the source of the conflict, but even when I remove the array from my plugin and enable post thumbnails for all post types, the conflict remains unchanged.

    So I am assuming that when a theme specifies certain post types to be enabled with this feature, it overrides that setting in plugins?

    Does anyone know anything more about adding post-thumbnails that could help me with this problem?

    My only idea right now would be to check if the theme or another plugin has added support for this already, and if it has added an array, filter that array to include my custom post type. Would something like this even be possible?
    "The future is already here—it's just not evenly distributed." - Gibson

  2. #2
    JLeuze's Avatar
    JLeuze is offline Here For The Peanuts
    Join Date
    Jan 2009
    Location
    Minnesota
    Posts
    125

    Default

    I did a bit more experimenting, figured out that the theme's settings for the post thumbnail doesn't necessarily have to override the plugin settings.

    It's just a matter of timing, the themes that I tested are running this function on a later action than the function I have setup in my plugin.

    I could run my function on a later action, but this is either going to override the theme or another plugin, or it is going to open up post thumbnails to all post types.

    I'd prefer to cleanly add featured image support just to my own post type and not mess with any other post types, so I don't think the adjusting the timing of my function would be of much help.
    "The future is already here—it's just not evenly distributed." - Gibson

  3. #3
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Run later, add whatever you need directly to global $_wp_theme_features (see add_theme_support() source) to what theme already did?
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  4. #4
    JLeuze's Avatar
    JLeuze is offline Here For The Peanuts
    Join Date
    Jan 2009
    Location
    Minnesota
    Posts
    125

    Default

    Thanks Rarst, that's a good idea, I didn't think to dig into theme.php to see what was going on.

    Right now I am running this pretty early, on the "plugins_loaded" action. I guess I can't anticipate when every theme or plugin is running this, but timing my action to run after the theme's functions.php would cover the most common conflicts.

    I can't find any info on when functions.php runs, I'm guessing "setup_theme", any know if that is right?

    It looks like this is the bit of code in theme.php that is setting up the post thumbnails:

    PHP Code:
    function current_theme_supports$feature ) {
                global 
    $_wp_theme_features;
        
                if ( !isset( 
    $_wp_theme_features[$feature] ) )
                        return 
    false;
        
                
    // If no args passed then no extra checks need be performed
                
    if ( func_num_args() <= )
                        return 
    true;
        
                
    $args array_slicefunc_get_args(), );
        
                
    // @todo Allow pluggable arg checking
                
    switch ( $feature ) {
                        case 
    'post-thumbnails':
                                
    // post-thumbnails can be registered for only certain content/post types by passing
                                // an array of types to add_theme_support().  If no array was passed, then
                                // any type is accepted
                                
    if ( true === $_wp_theme_features[$feature] )  // Registered for all types
                                        
    return true;
                                
    $content_type $args[0];
                                if ( 
    in_array($content_type$_wp_theme_features[$feature][0]) )
                                        return 
    true;
                                else
                                        return 
    false;
                                break;
                }
        
                return 
    true;

    Not exactly sure how to work directly with this, I'm pretty much a newbie when it comes to PHP, could anyone point me in the right direction?
    "The future is already here—it's just not evenly distributed." - Gibson

  5. #5
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Right now I am running this pretty early, on the "plugins_loaded" action. I guess I can't anticipate when every theme or plugin is running this, but timing my action to run after the theme's functions.php would cover the most common conflicts.
    functions.php is running earlier in same file (wp-settings.php).

    PHP Code:
    /**
     * Allows a theme to register its support of a certain feature
     *
     * Must be called in the theme's functions.php file to work.
     * If attached to a hook, it must be after_setup_theme.
     * The init hook may be too late for some features.
     *
     * @since 2.9.0
     * @param string $feature the feature being added
     */
    function add_theme_support$feature ) {
        global 
    $_wp_theme_features;

        if ( 
    func_num_args() == )
            
    $_wp_theme_features[$feature] = true;
        else
            
    $_wp_theme_features[$feature] = array_slicefunc_get_args(), );

    Seems like you will need to check what $_wp_theme_features['post-thumbnails'] holds and modify accordingly. Also see comments for this function, might be better choice of hook to mess with this.
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  6. #6
    JLeuze's Avatar
    JLeuze is offline Here For The Peanuts
    Join Date
    Jan 2009
    Location
    Minnesota
    Posts
    125

    Default

    Thanks for pointing me at the right file, in wp-settings.php I found where it loads the theme's function file:

    PHP Code:
        // Load the functions for the active theme, for both parent and child theme if applicable.
        
    if ( TEMPLATEPATH !== STYLESHEETPATH && file_existsSTYLESHEETPATH '/functions.php' ) )
                include( 
    STYLESHEETPATH '/functions.php' );
        if ( 
    file_existsTEMPLATEPATH '/functions.php' ) )
                include( 
    TEMPLATEPATH '/functions.php' );
        
        
    do_action'after_setup_theme' );
        
        
    // Load any template functions the theme supports.
        
    require_if_theme_supports'post-thumbnails'ABSPATH WPINC '/post-thumbnail-template.php' ); 
    So it looks like if I want to run my function after the theme and have the post thumbnails work, I need to use the "after_setup_theme" action.

    At least when I setup my plugin to enable post-thumbnails for all post types and run on "after_setup_theme", it worked pretty well in my tests.

    Now if I can just rein that in to work on just slides
    "The future is already here—it's just not evenly distributed." - Gibson

  7. #7
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Purely theoretical code, but I imagine close to what you need

    PHP Code:
    function add_slide() {
        global 
    $_wp_theme_features;

        if( !isset( 
    $_wp_theme_features['post-thumbnails'] ) )
            
    $_wp_theme_features['post-thumbnails'] = array( array( 'slide' ) );

        elseif ( 
    true === $_wp_theme_features['post-thumbnails'] )
            
    $_wp_theme_features['post-thumbnails'] = array( array( 'post','page''slide' ) );

        elseif ( 
    is_array$_wp_theme_features['post-thumbnails'] ) )
            
    $_wp_theme_features['post-thumbnails'][0][] = 'slide';

    No promises if it will work. :)
    Last edited by Rarst; 07-09-2010 at 01:58 PM. Reason: fix for third condition; and second... tricky array to get right
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  8. #8
    JLeuze's Avatar
    JLeuze is offline Here For The Peanuts
    Join Date
    Jan 2009
    Location
    Minnesota
    Posts
    125

    Default

    Awesome, that code worked perfectly Rarst, I didn't have to change a thing!

    Works great with post thumbnails disabled, enabled, or any with any array I threw at it and totally fixed the specific theme conflict I was trying to fix for a user.

    Only situation in which it didn't work strangely enough was with 2010. Checked it out, 2010 is running this on "after_setup_theme" as well. Tested my action, and "after_setup_theme" is the latest you can run this and get the post thumbnail template to be included.

    So I set the priority of my action super low and it works just fine with 2010:

    PHP Code:
    add_action'after_setup_theme''meteorslides_featured_image_array''9999' ); 
    As long as no other themes or plugins try to come to the party even later than mine is, I think it should play nice with others

    And I thought that moving the featured image box was a learning experience. I should really write a blog post about using featured images with a custom post type, it ended up being a lot trickier than I expected.
    "The future is already here—it's just not evenly distributed." - Gibson

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts