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!

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


  1. Здравствуйте! 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 .

  2. Clemens Tolboom June 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.


  3. Rutger Gabriel April 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.

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

  5. 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’;

    Works like a charm!

    Sincere THANK YOU!!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All comments are held for moderation and only published when on topic and not rude. You'll even get little stars if you do an extra good job.

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.

icon-search icon-link