1

I am trying to figure out a way to use a custom template within my plugin.

I would like to avoid using a custom post type if possible. I searched for a way to add my theme file to the dropdown list in the admin (under page attributes), but nothing seems to work.

Here is what I have:

add_filter('page_template', 'catch_plugin_template');

function catch_plugin_template( $template ) {
if( is_page_template('page_template.php') ){
    $template = WP_PLUGIN_DIR . '/myplugin/page_template.php';
    return $template;   
}
} 

However, since I can never select "page_template.php" from the template list it wont fire. Any suggestions on how to fix this or another way to approach it? I have considered changing the template based on whether or not certain post meta has a value, but I can't get that to work.

The code above works fine if I remove the if statement but I'm hoping for some sort of viable alternative.

1
  • Please search the site (via Google) before asking. We've already had that question/answer. Example here, and another one is here
    – kaiser
    Commented Sep 15, 2012 at 9:39

1 Answer 1

0

Try following code it will work with if statement as well.

define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

function catch_plugin_template( $template ) {

    if( is_page_template('page_template.php') ){

        $template = MY_PLUGIN_PATH . '/page_template.php';

    } 

    return $template;   
} 
add_filter('template_include', 'catch_plugin_template');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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