3

Whenever you add a image gallery to a post, the images automatically link to the image attachment pages. This is not ideal, because I want to use Fancybox so the user can cycle through the images.

I tried the following code to let the images link to the files by default, but this doesn't seem to work for galleries. Any suggestions on how to set the default link type to "file" for image galleries?

I would be even happier if the image attachment page wouldn't be available at all (on galleries an regular images). I don't really like this feature.

/**
 * Set default image link type to file
 */
function tp_set_default_link_type() {
    update_option( 'image_default_link_type', 'file' );
}
add_action( 'after_switch_theme', 'tp_set_default_link_type' );

Thanks in advance!

6

You can override the gallery shortcode link attribute with:

add_filter( 'shortcode_atts_gallery',
    function( $out ){
        $out['link'] = 'file'; 
        return $out;
    }
);

This means that even if your shortcodes are:

[gallery ids="1,2,3"]
[gallery ids="1,2,3" link="none"]

the gallery output will always be generated as if you used link="file".

5

There is now (from version 4.0) much better solution which actually sets the default option:

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');

view: Overide Gallery Default Link to Settings

0

Based on birgire answer but more flexible and fix

the gallery output will always be generated as if you used link="file"

and still be able to use link="none" for exemple

/**
 * Set default link type to “file” for image galleries when link isn't set
 */
add_filter( 'shortcode_atts_gallery',
    function( $out ){
        if( !array_key_exists('link', $out)
            || empty($out['link'])
          )
          $out['link'] = 'file';

        return $out;
    }
);

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.