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.

2 s
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *