3

I created a plugin and added a custom post type to the plugin. I then created a template using the hierarchy single-{custom-post-type}.php. So my cpt slug is wppro-events and the single post template is called single-wppro-events.php. I also added to the top of the template file the comment section below but my post template is not being used for the cpt. Any idea why it is not being used. Also page attributes on the edit page isn't showing the option to change the template either. Astra is the theme I am using. Any idea why this isn't working?

/*
Template Name: Layout One
Template Post Type: wppro-events
*/

I also tried the below code but still didn't work:

/* Filter the single_template with our custom function*/
add_filter('single_template', 'my_custom_template');

function my_custom_template($single) {

    global $post;

    /* Checks for single template by post type */
    if ( $post->post_type == 'Event Items' ) {
        if ( file_exists( PLUGIN_PATH . '/single-wppro-events.php' ) ) {
            return PLUGIN_PATH . '/single-wppro-events.php';
        }
    }

    return $single;

}

Additional Information Requested

Register CPT code

function wppro_register_events_post_type() {
    $labels = array(
        'name' => __( 'Events', 'wppro' ),
        'singular_name' => __( 'Event', 'wppro' ),
        'add_new' => __( 'Add New Event', 'wppro' ),
        'add_new_item' => __( 'Add New Event', 'wppro' ),
        'edit_item' => __( 'Edit Event', 'wppro' ),
        'new_item' => __( 'New Event', 'wppro' ),
        'view_item' => __( 'View Event', 'wppro' ),
        'search_items' => __( 'Search Events', 'wppro' ),
        'not_found' => __( 'No events found', 'wppro' ),
        'not_found_in_trash' => __( 'No events found in trash', 'wppro' ),
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'wppro-events' ),
        'menu_position' => 20,
        'menu_icon' => 'dashicons-calendar',
        'supports' => array( 'title', 'editor', 'thumbnail' ),
    );

    register_post_type( 'wppro-events', $args );
}
add_action( 'init', 'wppro_register_events_post_type' );

The plugin is a standalone one in wp-contents/plugins. I put the template file in the plugin's root directory.

4
  • "my post template is not being used for the cpt" - how did you check that and what's the URL of the page you checked? "I created a plugin" - is your plugin a standalone one (in wp-content/plugins) or is it part of your theme? Is it a block-based or classic theme? Where did you put your template file - in the root theme folder, e.g. wp-content/themes/your-theme ? Can you add all that details, along with your post type registration code, to your question?
    – Sally CJ
    Commented Feb 19, 2023 at 5:09
  • @SallyCJ Added the requested info
    – JayDev95
    Commented Feb 24, 2023 at 16:17
  • What is PLUGIN_PATH set to? (I assume it's the root directory of your plugin.) Also: the Template Hierarchy's auto-selection of template files only works for files in your active theme. To load a template file located in your plugin, you'll need to use a filter. If single_template isn't working, perhaps singular_template or the more generic template_include might do it.
    – Pat J
    Commented Feb 24, 2023 at 18:03
  • @JayDev95 as the current answer stated, you should use if ( $post->post_type == 'wppro-events' ), and your custom template would then be used for single post pages in your CPT. So I was just wondering, have you tried that and did it work? If yes, then is there something else you wanted to know?
    – Sally CJ
    Commented Mar 1, 2023 at 14:34

1 Answer 1

3
+25

A few points.

  • Custom template files for single posts (not pages) within plugin directory normally needs an action filter, so, you must use it in your case.
  • Page templates are different then custom templates for any custom or built-in post type. To have page templates under Attributes etc. or in Astra, you need files like page-template1.php or page-template2.php having template header comments. Note that these are page templates, not post templates. In your case, to let the WP automatically pick your custom template for a custom post type from within your plugin directory, keep using the template action filter that you are already using.
  • In your template filter, you are matching post_type which is right, however, the value for it is wrong. Change Event Items to wppro-events in the action filter in line if ( $post->post_type == 'Event Items' ) and then check.
  • Still seeing old template? Flush the permalinks by saving permalinks for once under Admin Dashboard > Settings > Permalinks and then check again.

Let me know how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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