1

I'm trying to modify slidestoshow parameter in CoBlocks Carousel block via my theme's functions. Everything in the Codex implies modifying/passing the variable but looking at the code, it appears it's an [anonymous function?] Forgive me if this term is incorrect.

I've tried both apply_filters() and add_filters() to replace the entire array rather than the individual key/value. I've tried removing the existing filter and adding it again but I'm not sure if the way it's coded is preventing me from making my modification.

Do I need to call the plugin Class CoBlocks_Settings?

Here is the original plugin code:

$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,
                            ),
                        ),
                    ),
                )
            )
        )
    )
);

First, I tried just overriding it with:

apply_filters( 'coblocks_post_carousel_settings',  array( ... ));

Second, here's where I'm at in Functions:

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 Answer 1

0

apply_filters() calls all callbacks added via add_filter() for the specific filter hook, which is coblocks_post_carousel_settings in your case, so instead of:

apply_filters( 'coblocks_post_carousel_settings', 'my_filter_coblocks_carousel' );

You should actually do:

// You should use add_filter() and not apply_filters().
add_filter( 'coblocks_post_carousel_settings', 'my_filter_coblocks_carousel' );

And, your callback should accept the slider settings passed by the plugin to apply_filters() — which then passes the settings (which is an array) to your callback. So define your callback like so and then just change the settings (or array items) that you wish to change:

function my_filter_coblocks_carousel( $settings ) {
    $settings['slidesToShow'] = 5;
    return $settings;
}

Please check the function reference (see links above) for more details, but basically, if you want others to filter something in your code, you use apply_filters() to let that something to be filtered by plugin or some custom code. And in your case, the (CoBlocks) plugin calls apply_filters() and you're part of that "others", thus you use add_filter() and not apply_filters(). I hope that makes sense? :)

Update: Examples for modifying the inner slidesToShow settings

So this is actually generic PHP stuff and it depends on you on how would you modify that inner slidesToShow in the responsive array items, but hopefully these help you:

// 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,
    ),
);
4
  • Thanks for the thorough reply. You mention passing the sidebar settings, but slidesToShow is not a sidebar option & not part of the original $args array at the top of the coBlocks file. Since it's just hardcoded and json-encoded directly within the apply_filter tag, this array is not really passed to the original $args/settings array, correct? -- Do I need to have the full assoc. array, like $settings[][]['slidesToShow'] since there are 3 breakpoints? Does $settings need to be renamed as an existing CoBlocks variable?
    – fourohfour
    Jan 20, 2021 at 7:14
  • Sorry, that "sidebar" was a typo and I meant to say, "slider". And in my answer, the point is that the plugin passes one parameter to the filter (coblocks_post_carousel_settings) callbacks and that parameter is an array with slidesToShow being one of the items, so you just need to change the value to whatever you want. And please, check the docs to further understand how apply_filters() and add_filter() work.
    – Sally CJ
    Jan 20, 2021 at 7:25
  • I hope the examples in my revised answer helps, and if my answer answered your question, please consider marking it as "correct" - just tick the check-mark next to my answer.
    – Sally CJ
    Jan 20, 2021 at 8:02
  • 1
    Thanks so much for your help and detailed replies. Sometimes it takes a little explanation to learn things. Much appreciated.
    – fourohfour
    Jan 21, 2021 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.