Darren Cooney
Darren Cooney

Reputation: 1060

Force WordPress Gallery to Use Media File Link instead of Attachment Link

Is there a filter or hook to force WordPress Galleries to use the Media File Link instead of the Attachment Link. I can not trust my client to manually switch the setting while creating the gallery. http://cl.ly/image/2g1L1G2c2222

Brings up another issue of why Media File is not the default type.

Upvotes: 4

Views: 3781

Answers (3)

Julien
Julien

Reputation: 1

I had the same problem for a long time and I found a solution. In the function.php file I had:

update_option( 'image_default_link_type', 'media' );

The value can be none, file or media. It works fine for me, if it can help.

Upvotes: 0

ToNo
ToNo

Reputation: 21

Obmerk Kronen's answer is very neat and works most of the situations. However, recently I ran across a problem on one of my client's websites, that couldn't be solved that way. I actually don't know, why. I suppose it was because of another gallery plugin, that was tampering with the same shortcode.

So, I found a different solution, not that neat, but working. Here, maybe someone will find this helpfull:

add_filter('the_content','galleries_attachments_to_media_links');
function galleries_attachments_to_media_links( $content ) {
/**
 * Overrides gallery shortcode settings and forces media file loading, if gallery set to attachment link
 */
if(strpos($content,'[gallery') !== FALSE) {
    $content = str_replace('[gallery','[gallery link="file"',$content);
    return $content;
}
else return $content;
}

It looks like even if a gallery is already set to media file, it's not a problem and WordPress executes the shortcode properly.

Upvotes: 2

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

You can basically ovveride the default gallery shortcode .

remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode
    add_shortcode('gallery', 'my_awesome_gallery_shortcode'); // add your own shortcode

    function my_awesome_gallery_shortcode($attr) {
    $output = 'Codex is your friend' ;
    return $output;
}

Inside the custom function you could set whatever you want .. look at the original gallery shortcode .

Another way is to actually filter the attributes ( if you will go to the above link, you will see a filter hook )

add_shortcode( 'gallery', 'my_gallery_shortcode' );

function my_gallery_shortcode( $atts )
{
    $atts['link'] = 'file';
    return gallery_shortcode( $atts );
}

Upvotes: 10

Related Questions