56

I'd like to offer my custom post type as a Plugin, so that people can use it without touching their theme folder. But custom post type templates -- such as single-movies.php -- reside in the theme folder. Is there a way to get WP to check for a single-movies.php in the plugin's folder? Hooking a function into the Filer Hierarchy? Or get_template_directory(); ?

88

You can use single_template filter hook.

/* 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 == 'POST TYPE NAME' ) {
        if ( file_exists( PLUGIN_PATH . '/Custom_File.php' ) ) {
            return PLUGIN_PATH . '/Custom_File.php';
        }
    }

    return $single;

}
  • It's awesome answer. Please share were can I get all the template available hooks like single_template. – Gowri May 2 '13 at 18:18
  • 6
    @gowri The filter is called by the get_query_template() function. It's a variable hook, so the first part {$type}_template changes depending on what parameter is passed to that function. See wp-includes/template.php for some common ones – shea May 7 '13 at 9:42
  • This tutorial covers the single_template hook with good explanation. Once you understand the hooks, @shea's comment suggests a simpler approach than @Bainternet's answer, using add_filter( 'single-movies_template', 'my_custom_template' ); as in the linked tutorial. (This question is closed to new answers, so I can't add it on its own.) – Greg Perham Sep 26 '17 at 4:34
  • 2
    You could replace PLUGIN_PATH with plugin_dir_path( __FILE__ ) and then remove the first slash from your file name. The full path would look something like this: return plugin_dir_path( __FILE__ ) . 'Custom_File.php'; – Frits Feb 13 '18 at 6:49
  • It could be PLUGIN_PATH is called WP_PLUGIN_DIR these days. – Hans Dash Mar 26 at 16:19
22

Updated answer

Cleaner and shorter version.

function load_movie_template( $template ) {
    global $post;

    if ( 'movie' === $post->post_type && locate_template( array( 'single-movie.php' ) ) !== $template ) {
        /*
         * This is a 'movie' post
         * AND a 'single movie template' is not found on
         * theme or child theme directories, so load it
         * from our plugin directory.
         */
        return plugin_dir_path( __FILE__ ) . 'single-movie.php';
    }

    return $template;
}

add_filter( 'single_template', 'load_movie_template' );

Old answer

Added a check for a custom post type specific template in theme folder to @Brainternet answer.

function load_cpt_template($template) {
    global $post;

    // Is this a "my-custom-post-type" post?
    if ($post->post_type == "my-custom-post-type"){

        //Your plugin path 
        $plugin_path = plugin_dir_path( __FILE__ );

        // The name of custom post type single template
        $template_name = 'single-my-custom-post-type.php';

        // A specific single template for my custom post type exists in theme folder? Or it also doesn't exist in my plugin?
        if($template === get_stylesheet_directory() . '/' . $template_name
            || !file_exists($plugin_path . $template_name)) {

            //Then return "single.php" or "single-my-custom-post-type.php" from theme directory.
            return $template;
        }

        // If not, return my plugin custom post type template.
        return $plugin_path . $template_name;
    }

    //This is not my custom post type, do nothing with $template
    return $template;
}
add_filter('single_template', 'load_cpt_template');

Now you can let the plugin users to copy the template from your plugin to their theme to override it.

With this example, templates must be in the root directory of both plugin and theme.

  • 1
    Using locate_template instead of get_stylesheet_directory sounds like the cleaner option to me (found here: code.tutsplus.com/tutorials/…). – Felix Jun 14 '16 at 18:11
  • 1
    The updated answer didn't work, the accepted answer (or your old answer) works. – Edward Mar 29 '18 at 14:43
  • 1
    You are right, @Edward, that was an horrendous update. I updated it again with Felix suggestion. Also this time I tested the code before posting it :) – campsjos Apr 3 '18 at 15:40
3

I would like to point out that when you are using the filter method for this, it is extremely important to prioritize the filter like so:

add_filter('single_template', 'my_custom_template', 99);

If you don't do this, sometimes WP will attempt to recheck after this filter. Was pulling my hair out because of this for like 2 hours.

-2

Above is a great answer, but checking for the $single var allows the template to override a template if one is provided by the theme/child theme.

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

function your_cpt_custom_template( $single ) {
    global $wp_query, $post;
    /* Checks for single template by post type */
    if ( !$single && $post->post_type == 'cpt' ) {
        if( file_exists( plugin_dir_path( __FILE__ ) . 'single-cpt.php' ) )
            return plugin_dir_path( __FILE__ ) . 'single-cpt.php';
    }
    return $single;
}
  • 1
    This does not check if single-cpt.php is present in a child or the theme. It only checks against single.php which will always be present in any themes. – newpxsn Oct 28 '15 at 20:01
  • That's right, when I wrote this I was using a really bare theme, there has to be a better way! – DigitalDesignDj Nov 20 '15 at 21:19

protected by Community Jan 16 '17 at 16:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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