gpt4 book ai didi

wordpress - 覆盖插件模板——子插件概念

转载 作者:行者123 更新时间:2023-12-02 08:25:31 30 4
gpt4 key购买 nike

是否可以通过在主题文件中创建文件夹并将文件复制到该文件夹​​中并更改内容来覆盖插件模板[文件夹和文件名都与插件中安排的名称相同]。

通常我们会按照这些系统更改 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' );

当然记得给你的函数加上前缀或者把它放在一个类中。这只是一个简单的例子。

未经测试。可能有些错误。

编辑:

只是为了回答所有问题。这正是 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,您可以创建这样的子插件。但是直接依赖于当前的插件代码是个坏主意。当您的父插件(1) 发生变化时,如果您正在使用已被删除或编辑的功能,您的插件将会中断。

但正如我所说,如果插件提供一致的 API,并且足以编写这样的功能,那么可以。但不适用于所有插件。至少无需编辑插件本身。

(1) 官方没有child pluginparent plugin之类的东西。

关于wordpress - 覆盖插件模板——子插件概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606985/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com