【问题标题】:How to filter pluggable function from wordpress child-theme如何从 wordpress 子主题中过滤可插入功能
【发布时间】:2015-03-11 23:06:30
【问题描述】:

我需要将过滤器添加到特定的 wordpress 函数,该函数在可插入函数主题的函数 f 包含的文件中定义

functions.php:

if (!function_exists('nectar_colors_css_output')) {
    function nectar_colors_css_output(){
        include('css/colors.php');
    }
}

colors.php:

<?php 
function nectar_colors() {
    // not relevant what happens here, but I have 
    // to call another function before this one is called!
}
?>

我使用一个子主题,当我尝试从子主题的functions.php 中过滤这个函数时,没有任何反应。这是因为父主题的可插入函数将在从子主题调用过滤器之后调用。

我在子主题的functions.php中的过滤功能是

function filter_function() {
  // some custom actions...
}
add_filter('nectar_colors', 'filter_function');

我怎样才能让这个过滤器工作?

【问题讨论】:

    标签: wordpress function filter themes pluggable


    【解决方案1】:

    您究竟想过滤什么?您可能误解了这些概念。

    以您提供的示例为例。父级在创建函数之前检查函数是否存在。 所以基本上,如果你在孩子的主题中定义它,父母就会知道它在那里并且不会创建默认主题。

    #themename-child
    
    function nectar_colors_css_output(){
        error_log("It worked!");
    }
    

    【讨论】:

    • 那会是一个解决方案,但它有点复杂:可插入的父主题函数包含另一个php脚本,其中包含我需要过滤的函数。我需要一个在使用我孩子的过滤器之前调用父主题函数的解决方案。我可以删除 if (!function_exists('nectar_colors_css_output')) { 但我不想更改父主题的文件!
    • 对不起我的错误,我改变了问题:)
    • 对此很抱歉,但我仍然不太明白您要做什么。我知道你不能修改父主题代码?在这种情况下,您只能使用他们提供的“钩子”。寻找do_action()apply_filter()
    • 是的,我不想修改父主题代码。问题是该钩子不适用于定义过滤器后包含(定义)的函数。
    • 您能否提供发生这种情况的实际代码?这会很有帮助。
    猜你喜欢
    • 2021-10-19
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多