Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to check for an appropriate template in the theme folder before falling back to the file in my plugin directory. Here’s my code:

add_filter('template_include', 'sermon_template_include');
function sermon_template_include($template) {
    if(get_query_var('post_type') == 'wpfc_sermon') {
        if ( is_archive() || is_search() ) :
           if(file_exists(TEMPLATEDIR . '/archive-wpfc_sermon.php'))
              return TEMPLATEDIR . '/archive-wpfc_sermon.php';
           return dirname(__FILE__) . '/views/archive-wpfc_sermon.php';
        else :
           if(file_exists(TEMPLATEDIR . '/single-wpfc_sermon.php'))
              return TEMPLATEDIR . '/single-wpfc_sermon.php';
           return dirname(__FILE__) . '/views/single-wpfc_sermon.php';
        endif;
    }
    return $template;
}

Problem is, it doesn’t work! :-) It always picks the file in my plugin folder. Any idea what to do? I’ve tried alot of variations but I can’t seem to get anything to work! Thanks in advance! Jack

EDIT

I'm expecting the archive-wpfc_sermon.php to be returned from the theme folder if it exists. However, the file from my plugin always gets returned. Thanks for your help! This is from my Sermon Manager plugin available in the repository.

share|improve this question
    
Which Theme template file is being returned, when you are instead expecting your Plugin template file to be returned? –  Chip Bennett Oct 28 '11 at 11:53
    
I'm expecting the archive-wpfc_sermon.php to be returned from the theme folder if it exists. However, the file from my plugin always gets returned. Thanks for your help @ChipBennett! This is from my Sermon Manager plugin available in the repository. –  Jack Oct 28 '11 at 15:53
add comment

2 Answers

up vote 5 down vote accepted

So, I'm not sure exactly what is causing the problem, but you might try the following:

  1. Plugin file path: replace dirname(__FILE__) with plugin_dir_path( __FILE__ )
  2. Theme file path: replace TEMPLATEDIR with get_stylesheet_directory()

It's possible that the problem comes from referencing the constants directly.

share|improve this answer
    
It works! Thank you so much!!!! –  Jack Oct 28 '11 at 16:38
add comment

I am not sure if this will work for you but it is worth a shot. I use this all the time for my custom post types when they require a special template.

// Template selection Defines the template for the custom post types.
function my_template_redirect()
  {
  global $wp;
  global $wp_query;
  if ($wp->query_vars["post_type"] == "your_custom_post_type")
  {
    // Let's look for the your_custom_post_type_template.php template 
   // file in the current theme
    if (have_posts())
      {
          include(TEMPLATEPATH . '/your_custom_post_type_template.php');
          die();
      }
      else
      {
          $wp_query->is_404 = true;
      }
    }
}

All you have to do is add this script in your functions.php file and put the template file in your theme directory.

This could be worth a shot and may not cause conflict with your plugin. However I am not sure of that.

I forgot ... dont forget to add the action. :)

add_action("template_redirect", 'my_template_redirect');
share|improve this answer
    
Thanks @Nicole, the code I posted works great to call my template file. However, I want to scan the current theme folder for a file of the same name before loading the one in my plugin folder. –  Jack Oct 28 '11 at 15:52
    
@Jack, So basically it calling your file from the plugin directory before the template in the theme directory at the moment? hmm going to have to give this one more thought. :D I love a good challenge! –  Nicole Oct 28 '11 at 15:59
    
That's right! It's really had me stumped. –  Jack Oct 28 '11 at 16:01
    
@Jack, This might sound like a dumb question so forgive me if it is. :) The code you have posted is that in the plugin or is that in the functions.php file? –  Nicole Oct 28 '11 at 16:03
    
@Jack The above code retrieves the template file from your theme directory. You can use an if statement to check if the 'include' worked, and if not, attempt to retrieve the template from the plug-in directory. –  Stephen Harris Oct 28 '11 at 16:07
show 2 more comments

Your Answer

 
discard

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.