I have a plugin that I am developing that is using custom post types and taxonomies. My question is this. How can I load the content/theme data from my plugin on to the page when the go to the custom url of the taxonomy?
EDIT
I am trying to use the plugin’s template files and not the themes for my custom taxomony.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First of all – plugins are for generating content, themes are for displaying it. So really, a plugin shouldn’t do this. But there are grey areas – for example in an ‘events’ related plugin, it would be desirable to display dates, venue etc – things that a WordPress theme wouldn’t normally display.
I would suggest
- making the plugin templates over-rideable with templates of the same name in the theme/child theme.
- Being able to ‘turn off’ plugin forcing of the template.
To alter the template being used you can use the template_include filter. This is an example for taxonomy templates, but a similar process is possible for custom post types.
add_filter('template_include', 'wpse50201_set_template');
function wpse50201_set_template( $template ){
//Add option for plugin to turn this off? If so just return $template
//Check if the taxonomy is being viewed
//Suggested: check also if the current template is 'suitable'
if( is_tax('event-venue') && !wpse50201_is_template($template))
$template = plugin_dir_url(__FILE__ ).'templates/taxonomy-event-venue.php';
return $template;
}
Note it assumes that the plugin templates are in a template sub-folder relative to current director.
Logic
This simply checks that the ‘event-venue’ taxonomy is being viewed. If not it will use the original template.
The wpse50201_is_template function will check if the template WordPress has picked from the theme/child-theme is called taxonomy-event-venue.php or taxonomy-event-venue-{term-slug}.php. If it is – the original template will be used.
This allows users of your plugin to copy them into their theme and edit them, and the plugin will prioritize the theme/child-theme templates. Only if it can’t find them does it fall back to the plug-in templates.
function wpse50201_is_template( $template_path ){
//Get template name
$template = basename($template_path);
//Check if template is taxonomy-event-venue.php
//Check if template is taxonomy-event-venue-{term-slug}.php
if( 1 == preg_match('/^taxonomy-event-venue((-(S*))?).php/',$template) )
return true;
return false;
}
I’ve used this method in a plugin – you can see a working example of the above here.
Method 2
This is how I’m calling a taxonomy template from a subdirectory within my theme folder. Keep in mind that taxonomy.php will need to stay in your root theme directory.
function call_taxonomy_template_from_directory(){
global $post;
$taxonomy_slug = get_query_var('taxonomy');
load_template(get_template_directory() . "/templates-taxonomy/taxonomy-$taxonomy_slug.php");
}
add_filter('taxonomy_template', 'call_taxonomy_template_from_directory');
For example, my taxonomy is called ‘news-category’. The template is located at wp-content/themes/mytheme/templates-taxonomy/taxonomy-news-category.php
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0