Wordpress - How to include a file using get_template_part() in a plugin?
get_template_part
is a theme function. You can't load plugin files with that function. Take a look at the source and you will notice the work is done by locate_template
. Look at that source and you will see that it always loads from theme directories.
However much you may want to use get_template_part
it is the wrong function.
You will need to include
your files.
The reason, so it seems to me, for get_template_part
is to allow themes to be extended-- aka, to ease the creation of child themes. Plugins are not intended to be extended in that way so there is no need for get_template_part
or for any plugin equivalent.
@s_ha_dum is correct that get_template_part
is a theme function, but he is incorrect that plugins are not intended to be extended in this way. It is simply more complicated.
This post by Pippin, describes how to use a function that will do the work of loading your plugin templates, while allowing users to override your plugin templates within their theme .
Essentially, it looks in a special folder in the theme, then if not found there, it looks within the templates folder for the plugin.
As was said before, you can't use get_template_part
in plugins, but there's a handy class on Github (created by Gary Jones) that mimics the get_template_part
functionality in plugins, adding the plugin to the fallback (child theme > parent theme > plugin).
In this way, you can override your plugin’s ”template part“ inside a child theme or a parent theme.
Usage (taken from the Github repo instructions):
- Copy
class-gamajo-template-loader.php
into your plugin. It can be into a file in the plugin root, or better, an includes directory. - Create a new file, such as
class-your-plugin-template-loader.php
, in the same directory. - Create a
class
in that file that extendsGamajo_Template_Loader
. - Override the class properties to suit your plugin. You could also override the
get_templates_dir()
method if it isn't right for you. - You can now instantiate your custom template loader class, and use it to call the
get_template_part()
method. This could be within a shortcode callback, or something you want theme developers to include in their files.
Example code:
// Template loader instantiated elsewhere, such as the main plugin file.
$meal_planner_template_loader = new Meal_Planner_Template_Loader;
// Use it to call the get_template_part() method. This could be within
// a shortcode callback, or something you want theme developers
// to include in their files.
$meal_planner_template_loader->get_template_part( 'recipe' );
// If you want to pass data to the template, call the set_template_data()
// method with an array before calling get_template_part().
// set_template_data() returns the loader object to allow for method chaining.
$data = array( 'foo' => 'bar', 'baz' => 'boom' );
$meal_planner_template_loader
->set_template_data( $data );
->get_template_part( 'recipe' );
// The value of bar is now available inside the recipe template as $data->foo.
// If you wish to use a different variable name, add a second parameter
// to set_template_data():
$data = array( 'foo' => 'bar', 'baz' => 'boom' );
$meal_planner_template_loader
->set_template_data( $data, 'context' )
->get_template_part( 'recipe', 'ingredients' );
// The value of bar is now available inside the recipe template as $context->foo.