Add WordPress Plugin Template Files

Creating a WordPress plugin where you need to create templates that can be overridden by the theme? Learn how to build a template loading system similar to WooCommerce allowing you to do just that!
Published
Updated
Typical Read
1 minutes
Sponsored
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.
Find your next web developer job
jobs by Indeed

Add WordPress Plugin Template Files was originally published on Apr 4, 2017 and has recently been updated on Aug 5, 2020 to reflect emerging trends.

If you’ve ever worked with WooCommerce theming, you’ve probably had to copy some of their templates files to your own theme in order to override the HTML. It’s a great system that allows frontenders the ability to change the output to match designs. In this article, I’ll walk you through how to do just that — easily add WordPress plugin template files.

Just wanna get straight to the code? Click here.


Let’s get started!

In this tutorial, we’ll create a templates folder within the plugin where we’ll add our… you guessed it, templates. Here we’ll place the templates that can be overridden in themes. These templates will be loaded via a custom plugin template function that will be called in place of WordPress’ get_template_part function.

Step 1: Locating the right template

In order for this to work, WordPress needs to be able to find the correct template file to load — the plugin or theme template. With the code below, WordPress will first look in the theme’s templates folder, if not found then will search in the main theme’s folder and then if still not found will fallback to the plugin’s templates folder.

Step 2: Loading the template file

Now that WordPress can find the right template, we need a function to load it. This is what will be used in place of get_template_part.

Step 3: Override the WordPress template file

Lastly, we’ll override the default WordPress template file with the code below.

Putting It All Together

Did you find Add WordPress Plugin Template Files useful? Get articles in your inbox.

…and don’t worry, I hate spam as much as you. Expect to hear from me at most once a week.

Latest Job Postings
Posted on Aug 9, 2020 at 9:17pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 9:15pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 1:19am
Littleton, CO
jobs by Indeed
Sponsored
Help support open-source projects & pro-bono non-profit services by donating today.
Sponsored
Do you have a great article to share you want to contribute? Contact me about your idea.
Sponsored
Help support open-source projects & pro-bono non-profit services by donating today.
Sponsored
Do you run a development agency, freelance developer or offer website services. Contact me today for information about advertising.
Sponsored
Sponsor my site by donating to help support open-source projects, like WordPress Zero Spam, Referrer Analytics & others.

6 Comments on “Add WordPress Plugin Template Files”

# Jul 1, 2018

Здравствуйте! Implement the following template tags to add WordPress-generated class attributes to body, post, and comment elements. For post classes, apply only to elements within The Loop .

Clemens Tolboom

# Jun 7, 2018

Thanks for this post as it helps me to get started.

Unfortunately there are some problems with it.

– What @john said (guard for Post Type).
– What is this function call to `contests_locate_template`?
– The function `PLUGIN_get_template` is never called!
– What is apply_filters( ‘PLUGIN_locate_template’,…) for? As there are no filters elsewhere defined I don’t get it.

Cheers!

Caroline

# Jun 2, 2020

To answer your questions Clemens, because I know you’re still just dying to know 😉

– contests_locate_template should be PLUGIN_locate_template. It’s just a little typo.
– Replace PLUGIN with your plugin slug or whatever.
– PLUGIN_get_template would be called by your plugin, it doesn’t need to be called in this code!
– Filters could be accessed by third-party and end users.

Rutger Gabriel

# Apr 5, 2018

Thanks for this example, great way to get started and does exactly what I was looking for.

Shoutout to John’s comment that suggests adding a check, this is quite necessary since any other page that is NOT a custom post type will give an error.

Fabien

# Aug 16, 2017

Hi, I was looking for how to do this, great, thank you 🙂 But to understand, can we have an example of usage? Thank you

John

# Aug 13, 2017

Of all the methods and techniques I have tried, and I have tried several, this one worked! To make it specific to only load my modified templates for my plugin, I added a check in the

“function PLUGIN_template_loader( $template )…” Just before testing for the template to load like this:

function PLUGIN_template_loader( $template ) {

$find = array();
$file = ”;
/* Is this concerning the “Basic Plugin” cpt? */

if (get_post_type() !== “my-basic-test-plugin”) { // (or whatever plugin/post type you want to create of course)
return $template;
}
if( is_singular() ):
$file = ‘single-basic-plugin.php’;
elseif( is_tax() ):
$file = ‘archive-basic-plugin.php’;
endif;

Works like a charm!

Sincere THANK YOU!!

Leave a Reply

Your email address will not be published. Required fields are marked *

All comments posted on 'Add WordPress Plugin Template Files' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.