0
votes

I want to "override" the default "gallery" shortcode (WordPress), but only if I have used a given parameter to that gallery shortcode.

For example:

[gallery ids="1,2,3"]

It has no parameter, so it will output the standard gallery code.

[gallery mode="custom" ids="1,2,3"]

It has my "mode" parameter, so it will output another shortcode.

To achieve it, I have created a "gallery" shortcode in functions.php file:

function get_new_gallery( $atts ) {
    extract( shortcode_atts( array(
        'mode' => '',
        'ids' => '',
    ), $atts ) );

    $code ="";
    if ($mode == "custom") {
        //* Output custom shortcode
        $code = '[custom_gallery ids="' . $ids . '"]';
    } else {
        //* Need to do nothing...but don't know how to do it
        $code = '[gallery ids="' . $ids . '"]'; /* Here's the problem, it causes a loop */
    }
    return do_shortcode($code);
}

add_shortcode( 'gallery', 'get_new_gallery' );

It works fine when I use the mode="custom" parameter. It just output the new shortcode: [custom_gallery...]

However, when not using the parameter it breaks because it enters in an infinite loop. In the code, there's a comment with the line that breaks it.

What I want is to execute the standard "gallery" shortcode if no parameter is entered. But given I've overwritten it...don't know how to "scape" from the loop and just execute the gallery.

Any help?

Thanks in advance.

1

1 Answers

1
votes

Maybe an alternative approach can help? What about a filter on the gallery shortcode. See references: https://codex.wordpress.org/Plugin_API/Filter_Reference/post_gallery

and:

https://wpbeaches.com/filtering-gallery-image-output-wordpress/