Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"]
share|improve this question
Now that we know what you'd like, can you tell us what you've tried? You are expected to have researched the problem and made an attempt at solving it before posting a question. I expect you will need to replace the default shortcode with one of your own construction. – s_ha_dum Apr 16 at 1:40

2 Answers

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 );
}
share|improve this answer
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 at 22:05
It doesn’t change the post shortcode, but the attributes the shortcode handler gets. – toscho Apr 17 at 22:08
Understood! It worked beautifully. Thanks! – Alexnl Apr 18 at 22:53
@Alexnl Click the check mark to mark the answer that solved your problem. – toscho Apr 19 at 4:59

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

share|improve this answer
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 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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