Archive

Posts Tagged ‘Adding template preogramatically’

Adding template page from plugin

April 18, 2011 Leave a comment

Hello readers,

I have found a very simple solutions to assign a template page to specific or all pages . According to my previous tutorial for creating the template page and placing into the wordpress current template.

For creating the template you need to add comment on the top of the page as known from my blog

/*
Template Name: new template
*/

Now what ever you want add the functionality to the page and then add header/footer as on your preference.
Now your template file is ready to run or attach to page. But now no need to copy that php file to the template folder of wordpress.
all you need to do is call the following hook

add_filter( 'page_template', 'catch_plugin_template' );

Now this will be called on every page/post load.

add_filter( 'page_template', 'catch_plugin_template' );
function catch_plugin_template($template)
{
if(is_page())
{
$template = WP_PLUGIN_DIR . '/yourpluginname/template-file.php';
return $template;
}
}

Now the above could will execute on every page and the template would be assigned to all the pages you have created.

Thank You for reading.