Plugin Template

Easily include and pass data to theme overridable plugin templates.

Requirements

This works generally the same way as the regular WPDev\Template\Template helper, but with one key difference.

Instead of passing in just the filename of the template, an absolute path to the plugin's template file should be passed in. This is the file that will be used if there isn't an overriding template found in the child/parent themes.

Override in Theme

To override a plugin's template file in your theme, the file must be located in templates/plugins/{{plugin-folder-name}}/{{name-of-template.php}}

For example if the plugin you're overriding is in a folder called my-plugin, then it will look for templates in the child/parent theme's templates/plugins/my-plugin/ folder. This is to help avoid template naming conflicts between plugins. It also has the benefit of keeping the templates folder organized.

Simple Include

            
                use WPDev\Template\PluginTemplate;

                PluginTemplate::include(__DIR__.'/reusable-component.php');
                // or with global function
                plugin_template(__DIR__.'/reusable-component.php');
            
        

Include with Data

            
                use WPDev\Template\PluginTemplate;

                PluginTemplate::include(__DIR__.'/reusable-component.php', ['title' => 'So Easy']);
                // or with global function
                plugin_template(__DIR__.'/reusable-component.php', ['title' => 'So Easy']);
            
        

API Reference

Public Methods
Method Summary
__construct Locates and renders a theme template. Falls back to the absolute $file_path passed in.
includeTemplate
create
static
For a more fluid syntax. Alternatively use Template::include() or Template::locate().
getTemplate
include
static
Include a theme template file. Optionally pass data.
locate
static
Locates a template file.
  • __construct

    __construct( string $file_path [, array $data ] )

    Summary
    Locates and renders a theme template. Falls back to the absolute $file_path passed in.
    Parameters
    file_path
    The absolute path to the file in the plugin. The basename will be used when searching in the themes.
    data
    Data to be passed to the template.
  • includeTemplate

    includeTemplate( )

  • create

    create( string $file_name [, array $data ] )

    Summary
    For a more fluid syntax. Alternatively use Template::include() or Template::locate().
    Parameters
    file_name
    data
    Returns

    $this

  • getTemplate

    getTemplate( )

  • include

    include( string $file_name [, array $data ] )

    Summary
    Include a theme template file. Optionally pass data.
    Parameters
    file_name
    The file name of the template.
    data
    Data to be passed to view. Will also be extracted into variables.
    Returns

    bool

    True if successfully included the template. Otherwise, false.

  • locate

    locate( string $file_name )

    Summary
    Locates a template file.
    Parameters
    file_name
    Returns

    string

    The path to the template file. Empty if none found.