0

Plugin in question: http://dimsemenov.com/plugins/royal-slider/wordpress/

Background: This is a great plugin, and it features a 'post' version where the slider pulls content from posts. However, the taxonomy for the posts that it's pulling has to be set at the slider settings level in WordPress, it can't be done via shortcode (that we know of). The plugin author does offer a way to override the default query here: http://help.dimsemenov.com/kb/wordpress-royalslider-advanced/wp-modifying-order-of-posts-in-slider

Aim: create a custom shortcode through which we can pass custom attributes, and specifically taxonomy, post-type.

Example of what the shortcode would look like: [rscustom rsid="2" rstax="home" rspt="carousel-slide"]

Our issue: probably a dumb one, but I can't get the shortcode variables to 'pass into' the query function, my code:

function rscustom_shortcode( $atts, $content = null ) {

    // Extract variables from shortcode attributes: post type, id, taxonomy
    $a = shortcode_atts( array(
        'rspt' => '',
        'rsid' => '',
        'rstax' => '',
    ), $atts );

    $rspt = trim($a['rspt']);
    $rsid = trim($a['rsid']);
    $rstax = trim($a['rstax']);

    // Execute the Royal Slider Query Override
    add_filter('new_royalslider_posts_slider_query_args', 'newrs_custom_query', 10, $rsid);

    function newrs_custom_query($args) {

        $args = array( 
            'post_type' =>  'carousel-slide',
            'orderby' => array(
                'menu_order' => 'ASC'
            ),
            'tax_query' => array(
                array(
                    'taxonomy' => 'carousel-slide-category',
                    'field'    => 'slug',
                    'terms'    => "$rstax", // THIS VARIABLE ISN'T PASSING INTO THE FUNCTION
                ),
            ),
        ); 
        return $args;
    };

    return get_new_royalslider($rsid);
}
add_shortcode( 'rscustom', 'rscustom_shortcode' );

The line where I have...

'terms' => "$rstax"

...seems to be the bit where I'm hung up. When I have the line as...

'terms' => 'home'

...everything works wonderfully. I've looked into global variables, but just managed to confuse myself more.

Any and all help would be very, very much appreciated. Thanks in advance!

1
  • try anon function with closure via "use" keyword. these things are powerful and can solve many issues not just yours.
    – Joel M
    Commented Feb 16, 2020 at 15:18

2 Answers 2

1

Two words - variable scope. Use a closure instead and pass $rstax through with use:

add_filter(
    'new_royalslider_posts_slider_query_args',
    function ( $args ) use ( $rstax ) {
        return array( 
            'post_type' =>  'carousel-slide',
            'orderby' => array(
                'menu_order' => 'ASC'
            ),
            'tax_query' => array(
                array(
                    'taxonomy' => 'carousel-slide-category',
                    'field'    => 'slug',
                    'terms'    => $rstax,
                ),
            ),
        )
    }
);
1

Thank you @TheDeadMedic. LEGEND!

A quick run-down:

1) To have the below code work, I created a custom post type 'carousel-slide,' which also has a custom taxonomy 'carousel-slide-category'

2) In the RoyalSlider slider management, I created a 'Posts Slider' (Admin Menu > RoyalSlider > Edit Sliders > Create New Slider). The ID in this case of the slider is 2.

3) Added the code below in the themes functions.php file

4) Executed the slider via the shortcode [rscustom rsid="2" rstax="my-tax-slug"]

The working code:

function rscustom_shortcode( $atts, $content = null ) {

    // Extract variables from shortcode attributes: post type, id, taxonomy
    $a = shortcode_atts( array(
        // 'rspt' => '', Dont' need now, but in case need to use other CPTs in future
        'rsid' => '',
        'rstax' => '',
    ), $atts );

    $rspt = trim($a['rspt']);
    $rsid = trim($a['rsid']);
    $rstax = trim($a['rstax']);

    // Add Query Argument Filter
    add_filter( 'new_royalslider_posts_slider_query_args',
        function ( $args ) use ( $rstax, $rspt ) {
            return array( 
                'post_type' =>  'carousel-slide', // $rspt 
                'orderby' => array(
                    'menu_order' => 'ASC'
                ),
                'tax_query' => array(
                    array(
                        'taxonomy' => 'carousel-slide-category',
                        'field' => 'slug',
                        'terms' => $rstax,
                    ),
                ),
            );
        } 
    );

    // Execute the Royal Slider Query Override
    function newrs_custom_query($args) {
        return array( 
            'post_type' =>  'carousel-slide',
            'orderby' => array(
                'menu_order' => 'ASC'
            ),
            'tax_query' => array(
                array(
                    'taxonomy' => 'carousel-slide-category',
                    'field' => 'slug',
                    'terms' => $rstax,
                ),
            ),
        ); 
    };

    return get_new_royalslider($rsid);
}
add_shortcode( 'rscustom', 'rscustom_shortcode' );

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.