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:


Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

Method 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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x