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.
Try following code it will work with if statement as well.