GaryJones/Gamajo-Template-Loader Template, giống woocommerce (ok)
https://github.com/GaryJones/Gamajo-Template-Loader
Ví dụ 1:
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\pw-sample-template-loader.php
<?php
/*
Plugin Name: PW Sample Template Loader
Description: Illustrates how to build a template file loaded into a plugin using the Gamajo Template Loader class
Author: Pippin Williamson
Version: 1.0
*/
define( 'PW_SAMPLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
if( ! class_exists( 'Gamajo_Template_Loader' ) ) {
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;
ob_start();
$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' );
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\class-gamajo-template-loader.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\class-pw-template-loader.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\templates\content-header.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\templates\content-middle.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\pw-sample-template-loader-plugin\templates\content-footer.php
C:\xampp\htdocs\wordpress5\wp-content\themes\twentytwentyone-child\pw-templates\content-header.php
Kết quả:

Ví dụ 2.1: Nhúng trực tiếp trong file hd-quiz\index.php khi update vẫn bị xóa 😂 và tiếp sau đây chúng ta sẽ nhúng nó vào child theme :(
C:\xampp\htdocs\wordpress5\wp-content\plugins\hd-quiz\index.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\hd-quiz\includes\template-edit.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\hd-quiz\class-gamajo-template-loader.php
C:\xampp\htdocs\wordpress5\wp-content\plugins\hd-quiz\class-pw-template-loader.php
C:\xampp\htdocs\wordpress5\wp-content\themes\twentytwentyone-child\pw-templates\image.php
Ví dụ 2.2:
C:\xampp\htdocs\wordpress5\wp-content\themes\twentytwentyone-child\functions.php
Gamajo Template Loader
A class to copy into your WordPress plugin, to allow loading template parts with fallback through the child theme > parent theme > plugin.
Description
Easy Digital Downloads, WooCommerce, and Events Calendar plugins, amongst others, allow you to add files to your theme to override the default templates that come with the plugin. As a developer, adding this convenience in to your own plugin can be a little tricky.
The get_template_part() function in WordPress was never really designed with plugins in mind, since it relies on locate_template() which only checks child and parent themes. So we can add in a final fallback that uses the templates in the plugin, we have to use a custom locate_template() function, and a custom get_template_part() function. The solution here just wraps them up as a class for convenience.
Installation
This isn't a WordPress plugin on its own, so the usual instructions don't apply. Instead:
Manually install class
Copy
class-gamajo-template-loader.phpinto your plugin. It can be into a file in the plugin root, or better, anincludesdirectory.
or:
Install class via Composer
Tell Composer to install this class as a dependency:
composer require gamajo/template-loaderRecommended: Install the Mozart package:
composer require coenjacobs/mozart --devand configure it.The class is now renamed to use your own prefix, to prevent collisions with other plugins bundling this class.
Implement class
Create a new file, such as
class-your-plugin-template-loader.php, in the same directory.Create a class in that file that extends
Gamajo_Template_Loader(or the new prefixed name, if you installed via Composer/Mozart). You can see the Meal Planner Template Loader example class below as a starting point if it helps.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.
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.If you want to pass data to the template, call the
set_template_data()method with an array before callingget_template_part().set_template_data()returns the loader object to allow for method chaining.The value of
baris 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():The value of
baris now available inside the recipe template as$context->foo.This will try to load up
wp-content/themes/my-theme/meal-planner/recipe-ingredients.php, orwp-content/themes/my-theme/meal-planner/recipe.php, then fallback towp-content/plugins/meal-planner/templates/recipe-ingredients.phporwp-content/plugins/meal-planner/templates/recipe.php.
Meal Planner Example Class
Usage Example
The Cue plugin from AudioTheme uses this class. Starting at https://github.com/AudioTheme/cue/tree/develop/includes, it has this class in the vendor directory, then the required subclass of my class in the class-cue-template-loader.php file, which sets a few basic properties. It also has a template in https://github.com/AudioTheme/cue/tree/develop/templates.
If you wanted the playlist to have different markup for your theme, you'd copy templates/playlist.php to wp-content/themes/{your-active-theme}/cue/playlist.php and do whatever changes you wanted. WordPress will look for that file first, before then checking a parent theme location (if your active theme is a child theme), before falling back to the default template that comes with the Cue plugin.
Change Log
See the change log.
License
Contributions
Contributions are welcome - fork, fix and send pull requests against the develop branch please.
Credits
Built by Gary Jones Copyright 2013 Gary Jones
Last updated
Was this helpful?