覆盖插件模板——子插件概念
Over write plugin templates -- Child plugin concept
是否可以通过在主题文件中创建一个文件夹并将文件复制到该文件夹中并更改内容来覆盖插件模板[文件夹和文件名都与插件中安排的名称相同]。
通常我们会按照这些系统更改 woocommerce 页面模板。Woothemes 已经为他们的 woocommerce 插件实现了类似的东西。您将文件从 /wp-content/plugins/woocommerce <-->复制到<--> /wp-content/themes/yourthemes/woocommerce 并且 WP 自动使用主题文件夹中的文件而不是插件文件夹中的文件。通过这种方式,用户可以对他们的插件进行自定义,而不会因插件更新而丢失。所有插件都可以吗?
如果没有,是什么代码让 woocommerce 插件根据我们的主题文件夹 woocoomerce 文件夹中的文件更改样式?
或者 CHILD PLUGIN 概念怎么样?
is there any possible way ?
Woocommerce 首先检查文件 /wp-content/themes/yourthemes/woocommerce
是否存在并要求它。如果不是,它需要来自 /wp-content/plugins/woocommerce
的通用模板
插件中的简单可能解决方案,您可以使用。
function loadTemplate( $template_name ){
$plugin_path = plugin_dir_path(__FILE__);
$original_template = $plugin_path . "templates/" . $template_name . ".php";
$theme_path = get_template_directory();
$override_template = $theme_path . "/myplugin/" . $template_name . ".php";
if(file_exists($override_template)){
include( $override_template );
}
else{
include( $original_template );
}
}
现在您可以像这样使用 loadTemplate()
函数:
function load_teplate_after_content( $template_name ) {
loadTemplate( 'example' );
// Now it will check first for
// wp-content/themes/yourtheme/myplugin/example.php
// and load, if not exists it will load original template from
// /wp-content/plugins/myplugin/templates/example.php
}
add_filter( 'the_content', 'load_teplate_after_content' );
当然记得给你的函数加上前缀或者把它放在Class中。这只是一个简单的例子。
未测试。可能有些错误。
编辑:
只是为了回答所有问题。这正是 woocommerce 是如何做到的
/**
* Get template part (for templates like the shop-loop).
*
* @access public
* @param mixed $slug
* @param string $name (default: '')
* @return void
*/
function wc_get_template_part( $slug, $name = '' ) {
$template = '';
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
}
// Get default slug-name.php
if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
$template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
}
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
}
// Allow 3rd party plugin filter template file from their plugin
if ( ( ! $template && WC_TEMPLATE_DEBUG_MODE ) || $template ) {
$template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
}
if ( $template ) {
load_template( $template, false );
}
}
编辑 2:
Is that possible for all plugin
所有实现此功能的插件都是可能的。
or What about CHILD PLUGIN concept ?
没有统一的答案。如果插件为其功能提供 API,您可以创建这样的子插件。但是直接依赖于当前的插件代码是个坏主意。当您的 parent plugin
(1) 将要更改时,如果您使用的功能已被删除或编辑,您的插件将会中断。
但正如我所说,如果插件提供一致的API,并且足以编写这样的功能,那么是的。但不适用于所有插件。至少不用编辑插件本身。
(1)官方没有child plugin
或parent plugin
这样的东西。
是否可以通过在主题文件中创建一个文件夹并将文件复制到该文件夹中并更改内容来覆盖插件模板[文件夹和文件名都与插件中安排的名称相同]。
通常我们会按照这些系统更改 woocommerce 页面模板。Woothemes 已经为他们的 woocommerce 插件实现了类似的东西。您将文件从 /wp-content/plugins/woocommerce <-->复制到<--> /wp-content/themes/yourthemes/woocommerce 并且 WP 自动使用主题文件夹中的文件而不是插件文件夹中的文件。通过这种方式,用户可以对他们的插件进行自定义,而不会因插件更新而丢失。所有插件都可以吗?
如果没有,是什么代码让 woocommerce 插件根据我们的主题文件夹 woocoomerce 文件夹中的文件更改样式?
或者 CHILD PLUGIN 概念怎么样?
is there any possible way ?
Woocommerce 首先检查文件 /wp-content/themes/yourthemes/woocommerce
是否存在并要求它。如果不是,它需要来自 /wp-content/plugins/woocommerce
插件中的简单可能解决方案,您可以使用。
function loadTemplate( $template_name ){
$plugin_path = plugin_dir_path(__FILE__);
$original_template = $plugin_path . "templates/" . $template_name . ".php";
$theme_path = get_template_directory();
$override_template = $theme_path . "/myplugin/" . $template_name . ".php";
if(file_exists($override_template)){
include( $override_template );
}
else{
include( $original_template );
}
}
现在您可以像这样使用 loadTemplate()
函数:
function load_teplate_after_content( $template_name ) {
loadTemplate( 'example' );
// Now it will check first for
// wp-content/themes/yourtheme/myplugin/example.php
// and load, if not exists it will load original template from
// /wp-content/plugins/myplugin/templates/example.php
}
add_filter( 'the_content', 'load_teplate_after_content' );
当然记得给你的函数加上前缀或者把它放在Class中。这只是一个简单的例子。
未测试。可能有些错误。
编辑:
只是为了回答所有问题。这正是 woocommerce 是如何做到的
/**
* Get template part (for templates like the shop-loop).
*
* @access public
* @param mixed $slug
* @param string $name (default: '')
* @return void
*/
function wc_get_template_part( $slug, $name = '' ) {
$template = '';
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
}
// Get default slug-name.php
if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
$template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
}
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
}
// Allow 3rd party plugin filter template file from their plugin
if ( ( ! $template && WC_TEMPLATE_DEBUG_MODE ) || $template ) {
$template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
}
if ( $template ) {
load_template( $template, false );
}
}
编辑 2:
Is that possible for all plugin
所有实现此功能的插件都是可能的。
or What about CHILD PLUGIN concept ?
没有统一的答案。如果插件为其功能提供 API,您可以创建这样的子插件。但是直接依赖于当前的插件代码是个坏主意。当您的 parent plugin
(1) 将要更改时,如果您使用的功能已被删除或编辑,您的插件将会中断。
但正如我所说,如果插件提供一致的API,并且足以编写这样的功能,那么是的。但不适用于所有插件。至少不用编辑插件本身。
(1)官方没有child plugin
或parent plugin
这样的东西。