• Is it possible to use shortcode_atts to give the gallery shortcode a new default image size?
    I understand how to add user values that can’t be changed in the gallery in the post, like this:

    function mj_gallery_atts( $atts ) {
            $atts['columns']    = '2';
            $atts['size']       = 'medium';
    
    return $atts;
    
    }
    add_filter( 'shortcode_atts_gallery', 'mj_gallery_atts', 10, 3 );

    But I would really like to just replace the defaults, so that the size and columns can still be overridden in the gallery shortcode in the post.

    Is this possible with shortcode_atts or do I need to use another method, like rewrite the entire gallery_shortcode function as in media.php, as has been suggested in older forum posts? Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    If you collect the second of the three parameters passed to your filter callback, it is the default attributes of the shortcode. The third is the attributes set in the post content. If there are no overriding attributes in the third parameter, feel free to set the returned attributes to your liking instead of the actual defaults.

    Thread Starter Marcy

    (@antigone7)

    Thank you bcworkz.

    This is working for me now.

    /* Set new defaults ( columns="2" and size="medium") and allows user input in the post gallery shortcode */
    function mjsd_gallery_atts( $out, $pairs, $atts ) {
    
            $atts = shortcode_atts( array(
                'columns' => '2',
                'size' => 'medium',
            ), $atts );
    
            $out['columns'] = $atts['columns'];
            $out['size'] = $atts['size'];
    
            return $out;
    
    }
    add_filter( 'shortcode_atts_gallery', 'mjsd_gallery_atts', 10, 3 );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Use shortcode_atts to replace default image size in gallery shortcode’ is closed to new replies.