Home of Scripts 'n Styles for WordPress, Backstage2D and History Keeper!

Including Page Templates from a WordPress Plugin

Recently, It occurred to me while writing a plugin dealing with custom post types and taxonomies for WordPress that it’d be nice to have some custom templates to go along with it. I seemed onerous to ask the the user to add template tags to their theme to be able to display a better page then what WordPress displays by default. So I set out to make the plugin include the template pages from the plugin directory itself. The code below is derived directly from the WordPress source code, which means it’s GPL, so feel free to use it in your GPL licensed projects.

So the first thing I needed to figured out was what functions needed to be filtered to let me add the plugin directory to the locations that WordPress checks for. I eventually found the appropriate ones by reading the source code and asking in the #wordpress IRC chat (not #wordpress-dev, that’d be the wrong chat to ask this kind of question). “get_single_template()” and “get_taxonomy_template()” are the functions of interest and they in turn call “locate_template()” which is the function we need to rewrite.

The “locate_plugin_template()” function is a direct copy of “locate_template” function but I added the $this_plugin_dir variable (using code I found on the wordpress.org forums) and added a 3rd check to the foreach loop. (Some of the code here can be removed, for example the $load check with the load_template() call, as our code won’t be invoking it but I left it to better reflect the source.)

Next we have to filter the appropriate functions to call our new function.

function get_custom_taxonomy_template($template){// Twenty Ten adds a 'pretty' link at the end of the excerpt. We don't need it for the taxonomy.
remove_filter('get_the_excerpt','twentyten_custom_excerpt_more');
remove_filter('get_the_excerpt','twentyten_auto_excerpt_more');

There’s some bonus remove_filter calls at the beginning and ‘locate_template’ is replace by ‘locate_plugin_template’. The only other thing is that we simply return the $template variable instead of using ‘apply_filters’ (I got errors when trying to apply the filter while running the filter filtering the filter :-/). There is also a check to see if we are working with our own taxonomy: The code in the if statement doesn’t really need to run again unless it’s one of our taxonomies, else it’ll just return the original $template.

Now, this isn’t a perfect solution. For example, themes have much varying structures, so building a template that’s compatible with Twenty Ten wouldn’t necessarily be compatible with any other theme. If the theme isn’t compatible, it could be bad since you’ve interrupted the fallback that the theme provides in favor of yours. You should probably include a check to see if the current theme is the theme you are targeting. I suggest you use this code in a plugin that is basically a companion plugin for your own theme or framework. Additionally, you could offer template tag functions, shortcodes, and widgets as a more robust solution.

6 thoughts on “Including Page Templates from a WordPress Plugin”

Interesting. I am trying to customise the “subscriptions manager” page of “subscribe-to-comments” plugin to my website’s theme. Will this be helpful? Wasn’t sure where to add this code:
To the functions.php of theme?
Or, create a separate file in the plugin directory? Shall be thankful for answer.

My code was intended for inclusion in a plugin, but I don’t intend anyone use my code directly… I’d have to be adjusted significantly to do whatever you need. I meant it only as an example of where to look for such info (each function has a reference link to the WordPress source code) primarily for intermediate plugin developers.

Prof.S.Balachandran: If you have access to the theme (as a theme dev or blog owner) you need not modify anything in the manner that I am. This probably isn’t very good code for what you’re attempting 🙂

Hi Jon, I didn’t see any obvious mistakes, but the gist doesn’t include the add_filter calls or your version of locate_plugin_template. I can say that my tutorial code is over 2 years old now (I’m sure you noticed that the function links are outdated at least). Core may have changed how it handles things since then. A quick look though, I see a new function `get_query_template` and some new filters there. There’s probably an opportunity for simplifying down what we are trying to do there.

I originally intended this tutorial to be more about how to read through core’s code, and write filters against what you find. I didn’t really intend for anyone to use this particular code, so I haven’t kept it updated. Sorry I couldn’t be more help.. I’ll see about writing an updated version of this tutorial.