DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Override Theme Template In Plugin

Override Theme Template In Plugin

Paul Underwood user avatar by
Paul Underwood
·
Nov. 11, 14 · Web Dev Zone · Interview
Like (0)
Save
Tweet
2.52K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial we are going to look at how you can override a theme template file from within a plugin.

When WordPress displays a piece of content it selects the theme template by using a hierarchy of files, for example if you want to view the content of the a single post the WordPress hierarchy is:

  • single-{post_type}.php
  • single.php
  • index.php

This works by searching for the single-{post_type}.php file in the theme, if it's not there then it will search of the single.php file, if that isn't there then it will just use the index.php file to display the content.

By default WordPress will search for these files inside the selected theme, but what if you have created a new post type and want to take over the theme HTML with a template inside the plugin.

There is a filter in WordPress that you can use to change the template file to display instead of the default WordPress template hierarchy. The WordPress filter we are going to use is single_template, the return of this filter is the template that will be displayed.

Below is code you can use to get a template file from within a plugin, simply add the following to your plugin and change the location of the template.

function change_post_type_template($single_template) 
{
     global $post;

     if ($post->post_type == 'custom-post-type') 
     {
          $single_template = plugin_dir_path( __FILE__ ) . 'templates/custom-post-type-template.php';
     }

     return $single_template;
}
add_filter( 'single_template', 'change_post_type_template' );
Template

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Determine if Microservices Architecture Is Right for Your Business
  • Java: Why Core-to-Core Latency Matters
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo