修改函数中的CoBlock过滤器

时间:2021-01-20 作者:fourohfour

我正在尝试修改slidestoshow 通过“我的主题”的函数在CoBlocks Carousel块中设置参数。Codex中的所有内容都意味着修改/传递变量,但看看代码,它似乎是一个[匿名函数?]如果这个术语不正确,请原谅我。

我两个都试过了apply_filters()add_filters() 替换整个数组而不是单个键/值。我已经尝试删除现有的过滤器并再次添加,但我不确定它的编码方式是否阻止我进行修改。

我需要调用插件类吗CoBlocks_Settings?

以下是原始插件代码:

$block_content = sprintf(
\'<div class="%1$s"><div class="coblocks-slick pb-8" data-slick="%2$s">\',
    esc_attr( $class ),
    esc_attr(
        wp_json_encode(
            /**
             * Filter the slick slider carousel settings
             *
             * @var array Slick slider settings.
            */
            (array) apply_filters(
                \'coblocks_post_carousel_settings\',
                    array(
                    \'slidesToScroll\' => 1,
                    \'arrow\'          => true,
                    \'slidesToShow\'   => $attributes[\'columns\'],
                    \'infinite\'       => true,
                    \'adaptiveHeight\' => false,
                    \'draggable\'      => true,
                    \'responsive\'     => array(
                        array(
                            \'breakpoint\' => 1024,
                            \'settings\'   => array(
                                \'slidesToShow\' => 3,
                            ),
                        ),
                        array(
                            \'breakpoint\' => 600,
                            \'settings\'   => array(
                                \'slidesToShow\' => 2,
                            ),
                        ),
                        array(
                            \'breakpoint\' => 480,
                            \'settings\'   => array(
                                \'slidesToShow\' => 1,
                            ),
                        ),
                    ),
                )
            )
        )
    )
);
首先,我尝试用以下内容覆盖它:

apply_filters( \'coblocks_post_carousel_settings\',  array( ... ));
其次,这里是我在函数中的位置:

apply_filters( \'coblocks_post_carousel_settings\', \'my_filter_coblocks_carousel\' );
function my_filter_coblocks_carousel() {
    $carousel = array(
        \'slidesToScroll\' => 1,
        \'arrow\'          => true,
        \'slidesToShow\'   => $attributes[\'columns\'],
        \'infinite\'       => true,
        \'adaptiveHeight\' => false,
        \'draggable\'      => true,
        \'responsive\'     => array(
            array(
                \'breakpoint\' => 1024,
                \'settings\'   => array(
                    \'slidesToShow\' => 4,
                ),
            ),
            array(
                \'breakpoint\' => 600,
                \'settings\'   => array(
                    \'slidesToShow\' => 4,
                ),
            ),
            array(
                \'breakpoint\' => 480,
                \'settings\'   => array(
                    \'slidesToShow\' => 4,
                ),
            ),
        ),
    );
    return $carousel;
}

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

apply_filters() 调用通过添加的所有回调add_filter() 对于特定的过滤器挂钩coblocks_post_carousel_settings 在您的情况下,请不要:

apply_filters( \'coblocks_post_carousel_settings\', \'my_filter_coblocks_carousel\' );
你实际上应该做:

// You should use add_filter() and not apply_filters().
add_filter( \'coblocks_post_carousel_settings\', \'my_filter_coblocks_carousel\' );
并且,回调应该接受插件传递给的滑块设置apply_filters() — 然后将设置(数组)传递给回调。因此,像这样定义回调,然后只需更改要更改的设置(或数组项):

function my_filter_coblocks_carousel( $settings ) {
    $settings[\'slidesToShow\'] = 5;
    return $settings;
}
请查看函数参考(参见上面的链接)以了解更多详细信息,但基本上,如果您希望其他人过滤代码中的某些内容,您可以使用apply_filters() 让插件或一些自定义代码过滤这些内容。在您的情况下,(CoBlocks)插件调用apply_filters() 而你就是其中的一员。”;“其他”;,因此,您使用add_filter() 而不是apply_filters(). 我希望这有意义?:)

更新:修改内部slidesToShow 设置

所以这实际上是通用的PHP内容,它取决于您如何修改内部slidesToShowresponsive 排列项目,但希望这些项目能帮助您:

// So this would all be in the my_filter_coblocks_carousel() function:

// Change the slidesToShow setting **only** for the "600" breakpoint.
foreach ( $settings[\'responsive\'] as $i => $arr ) {
    if ( 600 === $arr[\'breakpoint\'] ) {
        $settings[\'responsive\'][ $i ][\'settings\'][\'slidesToShow\'] = 3;
    }
}

// Add a new breakpoint.
$settings[\'responsive\'][] = array(
    \'breakpoint\' => 320,
    \'settings\'   => array(
        \'slidesToShow\' => 1,
    ),
);