WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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(); ?

share|improve this question
up vote 37 down vote accepted

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 $wp_query, $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;
share|improve this answer
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
@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

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

function load_person_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_person_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.

share|improve this answer
Using locate_template instead of get_stylesheet_directory sounds like the cleaner option to me (found here: code.tutsplus.com/tutorials/…). – Felix Jun 14 at 18:11

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;
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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