You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过自定义插件实现Dokan插件tmpl-add-product-popup模板定制

Dokan Lite 自定义插件重写模板不生效解决方案

核心失效原因

绝大多数情况下钩子不生效是因为挂载时机不对:你直接在插件初始化阶段挂载dokan_locate_template钩子时,Dokan插件还未完成加载,钩子本身不存在,自然不会触发你的回调逻辑。

完整可运行实现方案

1. 插件目录结构参考

在你的自定义插件根目录下新建templates文件夹,将修改好的tmpl-add-product-popup.php放入该目录,结构如下:

wp-content/plugins/your-custom-plugin/
├── your-custom-plugin.php // 插件主文件
└── templates/
    └── tmpl-add-product-popup.php // 你自定义的Dokan模板

2. 插件主文件代码

将以下代码写入你的插件主文件即可:

<?php
// 插件头注释请根据你的实际需求修改
class Custom_Dokan_Template {
    public function __construct() {
        add_action('plugins_loaded', [$this, 'register_hooks']);
    }

    public function register_hooks() {
        // 先判断Dokan已激活再挂载钩子,避免站点报错
        if (!class_exists('WeDevs_Dokan')) {
            return;
        }
        add_filter('dokan_locate_template', [$this, 'override_dokan_template'], 99, 3);
    }

    public function override_dokan_template($template, $template_name, $template_path) {
        // 仅匹配目标模板,其他模板走默认逻辑
        if ($template_name === 'tmpl-add-product-popup.php') {
            $custom_template_path = plugin_dir_path(__FILE__) . 'templates/' . $template_name;
            if (file_exists($custom_template_path)) {
                return $custom_template_path;
            }
        }
        return $template;
    }
}

new Custom_Dokan_Template();

排查清单

如果按照上述代码配置后仍未生效,按以下顺序排查:

  • 优先级调整:如果有其他插件/主题也在修改Dokan模板,将add_filter的第三个参数(优先级)从99调整为更高值,比如999
  • 模板名校验:在回调函数开头临时添加var_dump($template_name);exit;,访问添加商品弹出页,确认输出的模板名和你判断的完全一致
  • 文件权限:确认插件目录下的模板文件权限不低于644,服务器可读
  • 缓存清理:清理站点缓存、对象缓存以及Dokan插件自身的缓存后再测试

内容的提问来源于stack exchange,提问作者Husnain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:45:06