【问题标题】:How do I stop processing filters in WordPress?如何停止处理 WordPress 中的过滤器?
【发布时间】:2016-10-04 18:25:40
【问题描述】:

有一个用于 WordPress 的联系表单 7 插件的扩展,我可以在其中隐藏某些表单字段,给定不同的条件(例如在选择选项设置为“其他”之前隐藏的“其他”字段)。不幸的是,如果其中一个字段被标记为必填,则会弹出一个错误提示该字段是必填的,即使用户从未见过该字段!

很遗憾,Contact form 7 设置了不可逆的字段失效:一旦字段失效,就无法重新验证,表单提交失败。由于 CF7 具有如此可扩展性,我需要一个涵盖所有使用 CF7 验证钩子的验证插件的解决方案。

remove_filters() 不起作用,因为我只希望它应用于一个字段,而不是所有字段。删除过滤器会破坏所有字段的验证,我不希望这样。

是否有任何方法可以将函数附加到将“中止”该钩子的钩子,使其停止处理仅为此 apply_filters() 实例的更多过滤器函数?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    根据http://blogs.ntm.org/stephen-narwold/2016/10/04/how-to-stop-processing-filters-in-wordpress/ 的帖子,这是我想出的。我们需要将$wp_filter['this_filter']的内部数组指针设置到末尾。

    add_filters('example_filter', 'my_stop_processing_filters_func', 2);
    function my_stop_processing_filters_func( $in ) {
        // Stops filters of this tag from being applied after the current priority level is finished
        global $wp_filter;
        if ( $in['skip-me'] ) {
            // using current_filter() allows one function to be used for multiple filter tags if necessary
            end( $wp_filter[ current_filter() ] );
        }
    
        // never forget to return the filtered variable!
        return $in;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2017-06-04
      • 2019-04-24
      相关资源
      最近更新 更多