1

I'm writing a custom post type as a plugin and trying to figure out how I can enable post thumbnails within plugin documentation/template instead of enabling it in the functions.php. I tried adding the code below inside custom-post-plugin.php and it doesn't seem to work. I still have to write the code within the functions.php to enable post thumbnails for my custom post

/ * ADD THUMBNAIL FOR POST
 * ------------------------------------------------------------------------ */
add_theme_support( 'post-thumbnails', array( 'post', 'custom-post' ) );

Sample code below

<?php
/*
Plugin Name: Example Custom Post Plugin
Description: Example post plugin for News and Press custom post
*/
?>

<?php
/* ------------------------------------------------------------------------ *
 * EXAMPLE CUSTOM POST
  ------------------------------------------------------------------------ */

function example () {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Example', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Example', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Example', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Example', 'twentythirteen' ),
        'all_items'           => __( 'All Post', 'twentythirteen' ),
        'view_item'           => __( 'View Post', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Post', 'twentythirteen' ),
        'add_new'             => __( 'Add Post', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Post', 'twentythirteen' ),
        'update_item'         => __( 'Update Post', 'twentythirteen' ),
        'search_items'        => __( 'Search Post', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'Example', 'twentythirteen' ),
        'description'         => __( 'Example', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies'          => array('category', 'post_tag'),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 4,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'example', $args );
}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/

add_action( 'init', 'example', 'example', 1 );

add_action( 'after_setup_theme', 'example_featured_image');
function example_featured_image() {
        add_theme_support( 'post-thumbnails', array( 'post', 'page', 'production_firm', 'example' ) );
}
4

You are most probably using add_theme_support() in the wrong context (hook).

Because you are registering the post type yourself, all you need to do is to add support for thumbnails in the supports array when you pass your your arguments to register_post_type()

supports

(array/boolean) (optional) An alias for calling add_post_type_support() directly. As of 3.5, boolean false can be passed as value instead of an array to prevent default (title and editor) behavior.

Default: title and editor

All you need to do is to pass thumbnail to the array

'supports' => ['title', 'editor', 'thumbnail'], // Add additional values as needed

You would also need to check if support for thumbnails is enabled by the theme, and if not, add post thumbnail support in your plugin.

|improve this answer|||||
  • I just checked my code and "thumbnail' is already added in the array. Here is my code if you want to check on it pastie.org/10764783 Not sure what I'm doing wrong, – clestcruz Mar 18 '16 at 6:10
  • Here is my updated code pastie.org/10764842 and it's still not working. I checked the "screen options" and featured image is not showing – clestcruz Mar 18 '16 at 7:31
  • Please post your code as an edit to your question. Sorry, your theme support should be hooked to after_setup_theme, not init. For somne silly reason I typed init. Your code should work. Have you flushed your permalinks?. If the thumbnail metabox still don't show, disable all plugins and test your plugin on a bundled theme – Pieter Goosen Mar 18 '16 at 7:37
  • Thank you sir, managed to make it work with your help :) – clestcruz Mar 18 '16 at 7:46
  • My pleasure, glad it worked. Enjoy and good luck with your project ;-) – Pieter Goosen Mar 18 '16 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.