通过插件覆盖WordPress主题页面

时间:2018-10-28 20:36:49

标签: wordpress wordpress-theming custom-wordpress-pages

我想使用自定义的wordpress插件覆盖主题页面,我尝试添加过滤器,但没有结果,我不想直接更改主题,因为这对每个开发人员都是个坏主意。 这是我的插件代码 我添加了一个过滤器来检查页面

<xsl:for-each select="dischargeBudgets">
    <xsl:variable name="cell_height">
        <xsl:value-of select="string-length(.) * 2.2"/>
    </xsl:variable>
    <fo:table-cell padding="2px" height="{$cell_height}mm" width="6mm"
               text-align="end"
               border-style="solid">
        <fo:block-container reference-orientation="90">
            <fo:block white-space="nowrap">
                <xsl:value-of select="."/>
            </fo:block>
        </fo:block-container>
    </fo:table-cell>
</xsl:for-each>

我试图用我的自定义页面替换主题页面

public function __construct(){
    add_filter('theme_file_path','customize_theme_pages', 99, 2 );
}

对我来说,这似乎是合乎逻辑的解决方案,但是我的网站没有任何更改

1 个答案:

答案 0 :(得分:1)

您可以使用woocommerce_locate_template过滤器来覆盖插件中的woocommerce模板文件。

一个简单的示例,假设您想用插件的checkout/form-checkout.php文件覆盖inc/checkout/form-checkout.php

function customize_theme_pages($template, $template_name, $template_path) {
    if ($template_name == 'checkout/form-checkout.php') {
        $template = plugin_dir_path( __FILE__ ) . 'inc/checkout/form-checkout.php';
    }
    return $template;
}

add_filter('woocommerce_locate_template', 'customize_theme_pages', 99, 3);

**我已经测试了上面的代码,并且可以在运行在 WordPress 4.9.8 上的 WooCommerce版本3.5.0 上工作。

如果要替换标准页面模板,则可以使用page_template过滤器。