Include Custom Post Type Templates From Plugin

While writing advanced plugins using custom post types sometimes it becomes necessary to completely take over the display of the particular custom post type display template. While using filters can work for most things sometimes a whole template must be included. One example of this would be if using the custom post type as an endpoint for an api request, where having any additional markup would cause the request to fail.

To include the template from the plugin directory the template_include filter has to be used. The code below will check in the current theme directory before including the one in the plugin folder. This allows for the theme to always be able to override the template. This also shows how to include multiple templates, using simple conditional statements like is_single.

About Jeremy Clark

Small town IT worker with interests in all things technological and technical. Biggest interests are in web development especially the WordPress publishing platform and the community supporting it. Currently developing and maintain the free WordPress theme Techozoic. I'm also always available for hire.
This entry was posted in Featured, Internet, tech, wordpress and tagged , , , , . Bookmark the permalink.

10 Responses to "Include Custom Post Type Templates From Plugin"

Leave a reply