I’m trying to add template page from a plugin,and my template page is in this plugin folder.Is this posible?Here is my code:
global $user_ID;
$new_post = array(
'post_title' => 'Test Template Page',
'post_content' => 'Some text',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
if (!$post_id) {
wp_die('Error creating template page');
} else {
update_post_meta($post_id, '_wp_page_template', 'tp-file.php');
}
tp-file.php is my custom template page.When I put that file into the my-theme folder,it works fine,but I want to do this with file from plugin folder so I don’t have to force users to copy that file from plugin folder to template folder.
Is this possible?Any idea?
The article linked is on the right track, but i’ll make it more simple for you.. 😉
The filter basically looks to see if your special page template is set for the current page, if it is, updates the path to point at your plugins template instead.
Just be sure the path is right, else you’ll be seeing include errors… 🙂
Follow-up #1
Ok first problem is that WordPress validates any template set as a page template, ie. it checks to see if the file is in the theme folder, and is a valid template file, if not it skips past it and includes a more generic template, like page.php …
However, this doesn’t change the fact that the meta field still holds the value of your custom template, and also
is_page_template( 'tp-file.php' )
will correctly return true if used in place of my previous conditional statement, eg..NOTE: I’ve switched the code to use
WP_PLUGIN_DIR
as theWP_PLUGIN_URL
constant is not suitable for paths… (includes must use a path, not a URL).One issue, and this really isn’t something you can fix, is that when viewing the page from the admin area, when editting the page, the template won’t be listed as the active template, and saving changes could of course change the active template. There’s much we can do there, the page template dropdown is generated from a function that scans theme files, there’s no hooks in place that i can see that would allows us to extend the list with plugin templates.
Personally what i’d suggest, as a work-around would be to save an additional meta field with every page that created using your special plugin page, then add a hook onto
save_post
orwp_insert_post_data
and check if this meta field exists, if it does, also check the page template is set totp-file.php
, and if not, update it totp-file.php
. The additional meta field would just be a flag so to speak, to indicate which pages need to have your plugin template attached.Here’s your plugin working in it’s most basic form(yes i tested)… 🙂
Hope that helps clear things up.. 🙂
The
page_template
filter is deprecated now. (http://adambrown.info/p/wp_hooks/hook/page_template)Try using
single_template
(orarchive_template
for archive templates) instead.Based on @t31os’s answer:
Actually I found a hook in another thread that does exactly that:
WordPress: Can you programmatically create page templates?
I am pasting the code here for convenience:
Hope that helps someone.