I found a solution working for me by following this tutorial
In Short:
-
Create new file in plugin and copy Gamajo Template Loader Class
-
Create another file, creating your own class extension of Gamajo
<?php /** * Template loader for PW Sample Plugin. * * Only need to specify class properties here. * */ class PW_Template_Loader extends Gamajo_Template_Loader { /** * Prefix for filter names. * * @since 1.0.0 * @type string */ protected $filter_prefix = 'pw'; /** * Directory name where custom templates for this plugin should be found in the theme. * * @since 1.0.0 * @type string */ protected $theme_template_directory = 'pw-templates'; /** * Reference to the root directory path of this plugin. * * @since 1.0.0 * @type string */ protected $plugin_directory = PW_SAMPLE_PLUGIN_DIR;
}
-
Add this to your plugin
<?PHP define( 'PW_SAMPLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); require PW_SAMPLE_PLUGIN_DIR . 'class-gamajo-template-loader.php'; require PW_SAMPLE_PLUGIN_DIR . 'class-pw-template-loader.php'; function pw_sample_shortcode() { $templates = new PW_Template_Loader; } add_shortcode( 'pw_sample', 'pw_sample_shortcode' );
-
Create templates folder in your plugin root
-
Create template file.php in your plugins templates folder containing
the following example<h2><?php echo $data->section_title;?></h2>
Now you can access templates as following and load them with shortcode into frontend design plugins or your own custom themes while using your data to fill them
function pw_sample_shortcode() {
$templates = new PW_Template_Loader;
$args = array( 'section_title' => 'hello world' );
ob_start();
$templates->set_template_data($args); // assign variable array before calling templates
$templates->get_template_part( 'content', 'header' );
$templates->get_template_part( 'content', 'middle' );
$templates->get_template_part( 'content', 'footer' );
return ob_get_clean();
}
add_shortcode( 'pw_sample', 'pw_sample_shortcode' );