Support » Developing with WordPress » Add the support for post thumbnails in plugin

  • Resolved Guido

    (@guido07111975)


    Hi,

    In my theme the featured image is declared for posts only:

    
    add_theme_support( 'post-thumbnails', array( 'post') ); 
    

    I have a plugin that’s using a custom post type.
    How to add the featured image box for this custom post type as well?
    Because adding it to the supports array while registering the post type doesn’t work.

    In codex I’m reading this:

    Note: When you use custom post type that use thumbnails remember to check that the theme also supports thumbnails or use add_theme_support function.

    But add_theme_support must be called in the theme’s functions file to work (link).
    So what’s the proper way to include this in my plugin?

    Guido

Viewing 6 replies - 1 through 6 (of 6 total)
  • Perhaps you should use a Child Theme for this, if you’re using a theme that is not custom-made?

    Guido

    (@guido07111975)

    Hi,

    Yes, but I also want my plugin to support this, so would like to override this theme setting.

    Guido

    Moderator bcworkz

    (@bcworkz)

    Hey Guido! Have you tried adding theme support from an “after_theme_setup” action callback, from within your plugin? I don’t see anything in source code requiring functions.php. I think what the docs writers are getting at is trying to do this when plugins load is much too early, it must be done when or after themes are loaded. But from a specific file? I don’t think so.

    • This reply was modified 1 month, 4 weeks ago by bcworkz. Reason: typo
    Guido

    (@guido07111975)

    Hi BC!

    I don’t see anything in source code requiring functions.php.

    From Codex regarding add_theme_support:
    Must be called in the theme’s functions.php file to work.

    I have added the support in my plugin as well:

    
    add_theme_support( 'post-thumbnails', array( 'event') );
    

    And yes, it seems to work.
    But because of the info in Codex I have some doubts.

    So I have to look for the right moment to add this support from inside my plugin.

    Guido

    Moderator bcworkz

    (@bcworkz)

    I’m aware of what it says. But why would that be a requirement? I think it’s only so that if WP adds the featured image meta box to the post edit screen, the theme will actually do something with the selected image. It’d be a bad UX to offer the meta box but nothing comes of it. IMO, as long as your plugin is doing something meaningful with the selected image, it’s reasonable for WP to add the meta box.

    The “clean” alternative would be to check if the current theme supports featured images from the “after_theme_setup” hook, and if not, add your own similar meta box. Seems like a lot of extra effort since WP already has a perfectly good one.

    Guido

    (@guido07111975)

    Have thought about it some more… I came accross this issue with a custom made theme, that was only supporting featured images for posts. Guess this is not happening a lot, so I will leave it at that.

    Guido

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.