Automatically add this attribute to the gallery shortcode

When inserting a gallery it adds the following shortcode:


I would like it to automatically add link=”file” as the last attribute, whenever a shortcode is added. Like so:


Related posts

Leave a Reply

2 comments

  1. You can hijack the shortcode handler and set the attribute to a value of your choice. Then call the native callback for this shortcode.

    add_shortcode( 'gallery', 'file_gallery_shortcode' );
    
    function file_gallery_shortcode( $atts )
    {
        $atts['link'] = 'file';
        return gallery_shortcode( $atts );
    }
    
  2. There is a new shortcode_atts_{$shortcode} filter in WordPress 3.6 according to Mark Jaquith.

    You could use the shortcode_atts_gallery filter to force the link='file' attribute:

    add_filter('shortcode_atts_gallery','overwrite_gallery_atts_wpse_95965',10,3);
    function overwrite_gallery_atts_wpse_95965($out, $pairs, $atts){
        // force the link='file' gallery shortcode attribute:
        $out['link']='file'; 
        return $out;
    }
    

    when you have upgraded to 3.6.

    You can check it out in /wp-includes/shortcodes.php from the Core-Trac-Trunk:

    http://core.trac.wordpress.org/browser/trunk/wp-includes/shortcodes.php#L316