How to Override a Theme Template

Dikirimkan pada - Kali Terakhir Diubah Suai pada

There are lots of reasons why you might want to override your theme template through a plugin. An example is when you create custom post types. This override is achieved using the single template filter. It is used to load different customized templates for particular posts. Whenever it is implemented, the theme template is replaced for that custom post type.

To get a better understanding of how this works, we need to look at the way WordPress displays the template of your theme. It does so using a hierarchy. Take as an example the display of a single post. WordPress first looks in the theme for the single-{post_type}.php file. If it doesn’t find it, WordPress moves down to the next level in the structure and looks for the single.php file. When it cannot find that, it defaults to index.php.

Custom Post Types

Let’s go back to the example of creating a custom post type, where you want to allow a template inside a plugin to take over the site’s main theme template.

In the code below, the template file that you want to load is called your_new_post-type-template.php and is located in the plugins folder. Change this as necessary to customize it for your own website.

Also, in the example, the code will change the template file for all posts or pages that are of the type your_new_post_type. Again, you should change this to fit your own website.



function apply_the_post_type_template($single_template) {

     global $post;


     if ($post->post_type == 'your_new_post_type') {

          $single_template = dirname( __FILE__ ) . '/your_new_post-type-template.php';


     return $single_template;


add_filter( 'single_template', 'apply_the_post_type_template' );



So, what this code does is it loads the template called your_new_post-type-template.php for all posts assigned the type your_new_post_type. All other posts will get the default website template.


An Alternative – A Plugin


In some situations a plugin might achieve the desired result, and do it with less coding – particularly in relation to creating the template file. It does not work on all themes or with all plugins. Plus, it may not work if you are doing something specific with plugin on its own. But it is an option worth exploring if your objective is to have a different template display in certain sections of your website.


One example is the Jonradio Multiple Themes plugin. It allows you to override your main theme in several different situations. This includes by URL and by prefix URL. You can set the prefix as a wildcard, or you can have it search by a keyword in a URL. Or you can set it to override certain sections for the site by group. For example, you can override your main theme just on the homepage, or just for pages, or just for Posts.


It works by dynamically changing the active theme in your website. It will not work in all situations and is known to conflict with some premium themes that have a lot of functionalities. But it is an option to try if you want an easy to implement solution that allows you to display multiple themes within a WordPress website

Dipaparkan 18 Februari, 2015


Proofreader, Editor, Writer and App Developer

Do you need a professional editor and writer to proofread your technical documents, thesis, novel, statement of purpose, personal statement, resume, cover letter, manuscript, essay, short story, textbook content, or articles? Do you want to make sure that your marketing material content is flawless and appealing to readers? I can do any of that! I am a professional editor (academic, copy, line/su...

Artikel Seterusnya

The Difference Between Web Sites and Web Apps