Wordpress - Overide Gallery Default Link to Settings

There is an easier solution now:

function my_gallery_default_type_set_link( $settings ) {
    $settings['galleryDefaults']['link'] = 'file';
    return $settings;
}
add_filter( 'media_view_settings', 'my_gallery_default_type_set_link');

They added a filter to customize this default value (and other values related to the new media upload popup) in WP 4.0 (trac).


Old question but still getting search traffic so for everyone else:

You can accomplish what you describe by filtering the gallery shortcode attributes. Also useful if you want to change the default image size, columns, etc.

function gallery_should_link_to_files($out, $pairs, $atts)
{
    $atts = shortcode_atts( array( 
    'link' => 'file' 
    ), $atts );
    $out['link'] = $atts['link'];
    return $out;
}
add_filter('shortcode_atts_gallery', 'gallery_should_link_to_files', 10, 3);

Edit Jan 2020: As pointed out by @felwithe in comments, this code would go in your wp-includes/shortcodes.php, for example. It still works in current Wordrpess !

The filter seems poorly documented in the Codex : http://codex.wordpress.org/Function_Reference/shortcode_atts_gallery

Still, the option to modify the 'link' attribute was added early in 2013 : https://core.trac.wordpress.org/changeset/25665/trunk

Tags:

Gallery

Images