WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. antigone7
    Member
    Posted 8 months 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
    Member
    Posted 8 months 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. antigone7
    Member
    Posted 8 months 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 );

Reply

You must log in to post.

About this Topic