2 wordpress wordpress-plugin woocommerce woothemes
是否可以通过在主题文件中创建文件夹来覆盖插件模板,并将文件复制到该文件夹中并更改内容[文件夹和文件名都与插件中的名称相同].
通常我们通过遵循这些系统来改变woocommerce页面模板.Whehemes已经为他们的woocommerce插件实现了类似的东西.您将文件从/ wp-content/plugins/woocommerce < - > 复制到< - >/wp-content/themes/yourthemes/woocommerce,WP会自动使用主题文件夹中的文件而不是插件文件夹.通过这种方式,用户可以对其插件进行自定义,而不会将其丢失为插件更新.这是否适用于所有插件?
如果不是,什么代码使woocommerce插件根据我们的主题文件夹woocoomerce文件夹中的文件更改样式?
或者CHILD PLUGIN概念怎么样?
有什么办法吗?
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以像你想要的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' );
Run Code Online (Sandbox Code Playgroud)
当然记得给你的函数添加前缀或把它放在一个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 );
}
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:
这对所有插件都有可能吗?
所有具有此功能的插件都可以实现.
或者CHILD PLUGIN概念怎么样?
没有一般的答案.如果插件为其功能提供API,则可以创建此类子插件.但直接取决于当前的插件代码是个坏主意.如果您的parent plugin
(1)将要更改,如果您使用已删除或编辑的功能,您的插件将会中断.
但正如我所说,如果插件提供一致的API,并且它足以编写这样的功能,那么是的.但不适用于所有插件.至少没有编辑插件本身.
(1)没有这样的东西child plugin
或者parent plugin
正常的东西.
归档时间: |
|
查看次数: |
1403 次 |
最近记录: |