How to include a file using get_template_part() in a plugin?

A very simple question may be, but I’m struggling. In theme development, I worked with get_template_part() many times, and I understand its basics. But when I’s developing a plugin, I wondered using it showing me some errors:

Notice: Use of undefined constant STYLESHEETPATH – assumed ‘STYLESHEETPATH’ in ...wp-includestemplate.php on line 407

Read More

and

Notice: Use of undefined constant TEMPLATEPATH – assumed ‘TEMPLATEPATH’ in ...wp-includestemplate.php on line 410

Googling the problem showed a support fix:

But that seems a huge workaround – I doubt it. I think that shouldn’t be much complicated. I checked this WPSE Answer and found this line of code:

if ( '' === locate_template( 'loop-mycustomposttype.php', true, false ) )
    include( 'loop-mycustomposttype.php' );

Where there is a PHP include() function. As per as my WordPress knowledge I learned to prefer get_template_part() over PHP include(). Then how exactly I can use a simple get_template_part() in my plugin.

I’m not using any loop or something, I’m just separating (or you may say organizing) my plugin code into different files so that in some cases, I will simply comment them out to drop where they are not necessary. I tried:

get_template_part( 'my', 'special-admin' );

and then after the error, changed it to:

get_template_part( 'my', 'specialadmin' );

But you know that’s not the issue. I’m on local server, using WAMP.

Related posts

3 comments

  1. 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.

  2. @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.

  3. 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):

    1. Copy class-gamajo-template-loader.php into your plugin. It can be into a file in the plugin root, or better, an includes directory.
    2. Create a new file, such as class-your-plugin-template-loader.php, in the same directory.
    3. Create a class in that file that extends Gamajo_Template_Loader.
    4. Override the class properties to suit your plugin. You could also override the get_templates_dir() method if it isn’t right for you.
    5. 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.
    

Comments are closed.