3

When inserting a gallery it adds the following shortcode:

[gallery columns="6" ids="18,150,146,23,147,17,21,20,22"]

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

[gallery columns="6" ids="18,150,146,23,147,17,21,20,22" link="file"]
4

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 );
}
  • I added this to my functions.php file, but it didn't work. I got this shortcode in the page. [gallery ids="144,139,140,138,133,131,127"] – Alexnl Apr 17 '13 at 22:05
  • It doesn’t change the post shortcode, but the attributes the shortcode handler gets. – fuxia Apr 17 '13 at 22:08
  • @Alexnl Click the check mark to mark the answer that solved your problem. – fuxia Apr 19 '13 at 4:59
  • This one solved the issue. I clicked the checkmark. But I have only tested up to 3.5 I will add a further comment once I test on 3.6 – Alexnl Sep 24 '13 at 14:13
5

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

  • I'll try this when 3.6 is released at the end of April and I'll leave comment here on my success with it. – Alexnl Apr 17 '13 at 22:05
  • For some reason the link attribute is not passed to shortcode_atts, I filed a ticket here. – birgire Sep 24 '13 at 9:52
  • aha, a similar ticket has already been filed here, so hopefully this will work in version 3.7 – birgire Sep 24 '13 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.