I'm writing a plugin that creates a cpt.

I can include a template for viewing a single instance of the cpt using the single_template filter. And have it look for a template overload in the theme.

function my_cpt_single_template( $single_template ) {
    global $post;

    if ( $post->post_type == 'cpt' ) {

        $theme_template = locate_template('single-cpt.php');

        if( file_exists( $theme_template ) )
           $single_template = $theme_template;
           $single_template = PLUGIN_DIR . '/templates/single-cpt.php';


    return $single_template;
add_filter( 'single_template', 'mp_cpt_single_template' );

But this loads a template that doesn't take into account whatever the theme might be doing in single.php

So is there a way to just inject part of the current single-cpt.php into the theme's single.php? In a way that the template part is overloadable from the theme?

Filtering on the_content means the markup is hard-coded into the filter function, yes? So users would have to hack that function rather than overload the template part and adjust as necessary,

1 Answer 1


Theme templates are inherently not engineered to play well with plugins. As far as I remember there is still no clean and neat solution to shipping template in a plugin and have arbitrary theme pick it up.

There are some libraries, such as Gamajo Template Loader, that implement shipping templates in the plugin and allowing to override them. But as far as I understand you will still need to take care of calling template (part) in theme somewhere.

  • I've looked at Gamajo and pippin's article about it. It seems that cpts aren't well supported.
    – shanebp
    Apr 23, 2015 at 21:30

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.