3

I'm working through this tutorial on Custom Post Types (in a plugin) wherein the author demonstrates how to force the use of a dedicated template for the Custom Post Type. The author states thus:

The code (below) searches for the template single-movie-reviews.php in the current theme directory. If it is not found then it looks into the plugin directory for the template, which we supply as a part of the plugin. The template_include hook was used to change the default behavior and enforce a specific template.

Step 1: Added this code to my plugin-name.php file

add_filter( 'template_include', 'include_reviews_template', 1 );
function include_reviews_template(){
    if ( get_post_type() == 'reviews' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array( 'single-movie-reviews.php' ) ) ) {
                    $template_path = $theme_file;
                } else {
                    $template_path = plugin_dir_path( __FILE__ ) . '/templates/single-movie-reviews.php';
            }
        }
    }
    return $template_path;
}

Step 2: Create the single-{custom-post-type}.php file.

Next, I created the requisite single-movie-reviews.php file and added it to '/my-plugin/templates/' directory.

The author adds the following:

Note: You need to create a new page from the dashboard using the newly created template.

The Problems

  1. The Page template does NOT show in the dashboard
  2. When I visited the post I created using the Custom Post Type, it does show the post using the page template from the plugin. However... every other page (i.e. pages NOT using the CPT) now show a blank white screen (as though it cannot find the proper template to use).

So what am I doing wrong?

Please advise

  • Note, in your example of: $template_path = plugin_dir_path( __FILE__ ) . '/templates/single-movie-reviews.php'; that plugin_dir_path( __FILE__ ) already includes the trailing slash. So, as far as I know, the / before templates isn't necessary. – Garconis Mar 20 '17 at 16:33
3

Change this line:

function include_reviews_template(){

To this:

function include_reviews_template( $template_path ) {

When get_post_type() == 'reviews' is false you are returning an unset variable ($template_path). This change should send the value passed to the function by WordPress.

  • Dang! Missed that. Good catch! – sleeper Sep 28 '13 at 21:14
  • I also notice that problem #1 still exists: The Template Page is not showing in the list of templates. Given the code above, should that show? – sleeper Sep 28 '13 at 21:29
  • If you are you referring to the drop down list of page templates in the WordPress Page Editor, that list is updated by searching each template file for a line near the top with the text "Template Name:" in it. – Charles Clarkson Sep 28 '13 at 22:26
  • I guess it does not search the plugins directory(?), as I have a file setup as a template file in there. It's not seeing it. – sleeper Sep 28 '13 at 22:28
  • I don't know if there is a hook to have WP search other directories. – Charles Clarkson Sep 28 '13 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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