Dynamically adding filters

时间:2016-01-18 作者:Bryan Willis

我有一个函数,我试图在其中获取一个数组,以便在回调中使用它来动态添加过滤器。

下面是我需要从中检索数组键的函数(在$classes 变量):

function bw_add_markup_class( $attr, $context ) {

    $classes = array( 
        \'nav-primary\'     => \'navbar navbar-inverse navbar-static-top\',
        \'nav-secondary\'   => \'navbar navbar-default navbar-static-top\',
        \'site-header\'     => \'navbar navbar-static-top\'
    );
    $classes = apply_filters (\'bw_custom_classes\', $classes, $context, $attr);

    $value = isset( $classes[ $context ] ) ? $classes[ $context ] : array();
    if ( is_array( $value ) ) {
        $classes_array = $value;
    } else {
        $classes_array = explode( \' \', (string) $value );
    }
    $classes_array = array_map( \'sanitize_html_class\', $classes_array );
    $attr[\'class\'] .= \' \' . implode( \' \', $classes_array );
    return $attr;

}

Originally I was doing this to add my filters:

add_filter(\'genesis_attr_nav-primary\', \'bw_add_markup_class\', 10, 2);
add_filter(\'genesis_attr_nav-secondary\', \'bw_add_markup_class\', 10, 2);
add_filter(\'genesis_attr_site-header\', \'bw_add_markup_class\', 10, 2);
然而,我开始向$classes数组添加更多值,并且我还希望能够通过bw_custom_classes 不必写add_filter 为了他们。

这就是我被困的地方。虽然下面是一个改进,但它仍然没有完成我试图做的事情。

$classes_keys = array(
    \'nav-primary\',
    \'nav-secondary\',
    \'site-header\'
);
foreach($classes_keys as $context) {
    $context = "custom_attr_$context";
    add_filter($context, \'bw_add_markup_class\', 10, 2);
}
我的最佳猜测是:虽然我的php知识已经达到极限,但我还是希望能够做到这样。

add_action(\'after_setup_theme\', \'add_filters_to_front\');
function add_filters_to_front() {     
  $filters = bw_add_markup_class();
  foreach(array_keys($filters) as $context){
    $context = "genesis_attr_$context";
    add_filter($context, \'bw_add_markup_class\', 10, 2);
  }
}

1 个回复
最合适的回答,由SO网友:Fabien Quatravaux 整理而成

我认为你的思路是对的,但最后一部分很混乱。您正在使用bw_add_markup_class 功能有两个不同的用途:

要返回要添加的类,列出要调用的筛选器的上下文,如果我很清楚您想要执行的操作,您需要一个函数来返回数组,然后分别应用修改:

<?php
/**
 * Default array of classes to add separated from bw_add_markup_sanitize_classes()
 * This allows it to be used several places
 */
function bw_merge_genesis_attr_classes()
{
    $classes = array(
            \'content-sidebar-wrap\'      => \'row\',
            \'content\'                   => \'col-xs-12 col-sm-8 col-lg-7 col-lg-offset-1\',
            \'sidebar-primary\'           => \'hidden-xs col-sm-4 col-lg-3\',
            \'archive-pagination\'        => \'clearfix\',
            \'entry-content\'             => \'clearfix\',
            \'entry-pagination\'          => \'clearfix\',
            \'sidebar-secondary\'         => \'\',
    );
    if (has_filter(\'bw_add_classes\')) {
        $classes = apply_filters(\'bw_add_classes\', $classes);
    }
    return $classes;
}

/**
 * Adds Filters Automatically from Array Keys
 */
add_action(\'genesis_meta\', \'bw_add_array_filters_genesis_attr\');
function bw_add_array_filters_genesis_attr()
{
    $filters = bw_merge_genesis_attr_classes();
    foreach(array_keys($filters) as $context) {
        $context = "genesis_attr_$context";
        add_filter($context, \'bw_add_markup_sanitize_classes\', 10, 2);
    }
}

/**
 * Clean classes output
 */
function bw_add_markup_sanitize_classes($attr, $context)
{
    $classes = array();
    if (has_filter(\'bw_clean_classes_output\')) {
        $classes = apply_filters(\'bw_clean_classes_output\', $classes, $context, $attr);
    }
    $value = isset($classes[$context]) ? $classes[$context] : array();
    if (is_array($value)) {
        $classes_array = $value;
    }
    else {
        $classes_array = explode(\' \', (string)$value);
    }
    $classes_array = array_map(\'sanitize_html_class\', $classes_array);
    $attr[\'class\'].= \' \' . implode(\' \', $classes_array);
    return $attr;
}

/**
 * Adds classes array to bsg_add_markup_class() for cleaning
 */
add_filter(\'bw_clean_classes_output\', \'bw_modify_classes_based_on_extras\', 10, 3);
function bw_modify_classes_based_on_extras($classes, $context, $attr)
{
    $classes = bw_merge_genesis_attr_classes($classes);
    return $classes;
}

Usage in template or elsewhere:

现在您可以将类添加到数组中进行清理,它们也将自动添加到过滤器中。。。

add_filter(\'bw_add_classes\', \'bw_custom_example\');
function bw_custom_example($classes) {
    $new_classes = array( 
        \'nav-secondary\'  => \'navbar navbar-inverse navbar-static-top nav-blue\',
        \'entry\'          => \'panel panel-default\',
    );
    return wp_parse_args($new_classes, $classes);
}

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果