The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Use shortcode_atts to replace default image size in gallery shortcode (3 posts)

  1. Marcy
    Posted 2 years ago #

    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!

  2. bcworkz
    Posted 2 years ago #

    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.

  3. Marcy
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic