1

I've created a plugin that registers a custom post type named evento_type:

register_post_type( 'evento_type', ...

The CPT archive page is archive-evento_type.php and it is saved in the root directory of the plugin. It works like a charm. I think the single post page of the CPT should be named single-evento_type.php and that it should be saved in the root directory of the plugin, just like the archive page. I've created single-evento_type.php, but Wordpress keeps ignoring it and using the default post page instead, no matter how many

flush_rewrite_rules();

I scatter throughout the code and how many times I force permalinks recreation by manually switching the permalinks settings. WP keeps using my CPT archive page (which is the right thing) and happily ignoring my single post page.

I'm using WP 4.4.1 and the Wiz Theme, if that matters. I've googled a lot for this problem, but every solution I've found so far is about flushing rewrite rules or double-triple-billion-checking the files names, which I've already done.

How do I make WP use my single-evento_type.php instead of the default?

EDIT after Milo's comment: I wonder how could I be so blind. WP is not loading the archive template either, because I'm artificially generating the archive page with a standard page and a shortcode... I had forgot about this, er..., detail, and I was assuming WP was automagically finding my archive-evento-type.php file...

1
  • 1
    how are you loading the archive template from the plugin directory? that's not something that would happen without some sort of filter code loading it from there.
    – Milo
    Commented Jan 28, 2016 at 18:57

1 Answer 1

1

Any template file must be in the current active theme folder. If they are in a different location, they won't be automatically loaded, instead you need to code and modify the template file location determined by WordPress template system:

add_filter( 'template_include', 'envento_type_templates', 99 );
function portfolio_page_template( $template ) {

    if ( is_archive( 'evento_type' )  ) {
        // Full path to archive-evento_type.php file in
        // the plugin directoy
        $template = plugin_dir_path( __FILE__ ) . 'archive-evento_type.php';
    }

    if ( is_singular( 'evento_type' )  ) {
        // Full path to single-evento_type.php file in
        // the plugin directoy
        $template = plugin_dir_path( __FILE__ ) . 'single-evento_type.php';
    }

    return $template;
}

You could also use locate_template() to check if the template file already exists in theme. That would allow theme developers to override the default layout and desing created by the plugin.

add_filter( 'template_include', 'envento_type_templates', 99 );
function portfolio_page_template( $template ) {

    if ( is_archive( 'evento_type' ) && ! locate_template( 'archive-evento_type.php' ) ) {
        // Full path to archive-evento_type.php file in
        // the plugin directoy
        $template = plugin_dir_path( __FILE__ ) . 'archive-evento_type.php';
    }

    if ( is_singular( 'evento_type' ) && ! locate_template( 'single-evento_type.php' ) ) {
        // Full path to single-evento_type.php file in
        // the plugin directoy
        $template = plugin_dir_path( __FILE__ ) . 'single-evento_type.php';
    }

    return $template;
}

PD: it has nothing to do with permalinks.

2
  • Your answer is absolutely correct. It remains only to explain why CPT belong in plugins, as stated here [1] and agreed in many places around the www, but WP looks for their files in the theme by default... 1. justintadlock.com/archives/2013/09/14/… Commented Jan 28, 2016 at 19:59
  • 1
    Registering post types is one thing, how they are present is another thing. Data handlng should be done in plugins. Data presentation should be done in themes. Optionally, the plugin can include a fallback.
    – cybmeta
    Commented Jan 29, 2016 at 13:04

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.