Modifying a CoBlocks Filter in Functions

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;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x