I was a little surprised today when I discovered that the wordpress shortcode for "gallery" had no option to have the images be no link at all, either fixed to "File" or "Attachment Page". I'm not a fan of editing the core PHP, but this is definitely a wordpress shortage. Bummer about this hack is that if you upgrade WordPress, it'll likely overwrite this change, but hey, my blog will still be here for ya. Here's how to enable gallery link='none' to work:
Open up /wp-includes/media.php
Change line 760 in 2.9.2, and line 846 in 3.0 from:
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
to:
if(isset($attr['link']) && 'file' == $attr['link']){ $link = wp_get_attachment_link($id, $size, false, false); } elseif (isset($attr['link']) && 'none' == $attr['link']){$link = wp_get_attachment_image($id, $size, false);} else { $link = wp_get_attachment_link($id, $size, true, false); }
Whala - now you can use gallery link="none" and the images not be forced linked to something.
April 28th, 2010 - 08:54
I was looking for this solution, thank you! I was equally surprised that the image caption does not automatically go through a language filter. For multilingual blogs this is a big disadvantage of the built-in wordpress gallery, as I cannot use a caption at all. Do you perhaps know what apply_filter() tag should go and where in the media.php to run the caption text through a language filter? That would be a great help. I use Language Switcher, and normally, when I filter the post title, for example, I put apply_filter(‘the_title’, $post_title) in the function or plugin. I just simply have to find the echo/output. But with this image caption, I am really lost.
[Reply]
Sators reply on April 28th, 2010:
Well, I haven’t tested this, but I would look just a few lines down at 770 where you see wptexturize($attachment->post_excerpt). That’s the comment being injected…so maybe test throwing in your apply_filter around there…let me know what you come up with!
[Reply]
deschamps_24 reply on April 29th, 2010:
Thanks for the idea! I would not have thought of this, but I guess this just shows that I do not completely understand the workings of the gallery. Caption as post excerpt?
Anyway, I replaced the code you suggested with:
apply_filters(‘the_excerpt’, wptexturize($attachment->post_excerpt))
and it works like charm! So I managed to make my image gallery multi-lingual!!! Thank you!
[Reply]
May 16th, 2010 - 17:11
hi … when i use this fix on a 2.9.2 wp installation i return: Parse error: syntax error, unexpected T_VARIABLE [path]
any idea what the issue might be
[Reply]
joyce lukaczer reply on May 16th, 2010:
nevermind … got it [an extra space or two needed to be removed].
thanks much
[Reply]
May 22nd, 2010 - 23:24
would you happen to know how to modify the gallery shortcode so that all tags have wrapped around them ? I’ve been playing around in the media.php file for some time but I’m not particularly the smartest when it comes to php so I’m getting nowhere ;(
[Reply]
June 12th, 2010 - 11:01
Great stuff, it works very nicely !! I have been searching all over the web for this functionality for hours. You saved me a lot of time I would have put to draw tables and insert thumbnails without links manually…. Merci
[Reply]
June 30th, 2010 - 14:03
I can’t seem to get this working, same as joyce above I get Parse error: syntax error, unexpected T_VARIABLE
Unfortunately I don’t know how to sort this out
[Reply]
Sators reply on July 1st, 2010:
Make sure you copy everything as I have in the post into your file. This could be caused by a missing ; or }. Revert back to the original file and try again.
[Reply]
July 2nd, 2010 - 10:03
I used this hack but the image is now linked to the attachment page. Any help would be appreciated. If you need some data from my end, I am happy to share it.
[Reply]
Sarthak Singhal reply on July 2nd, 2010:
My shortcode looks like this [gallery columns="4" orderby="rand" link="none"]
[Reply]
Sators reply on July 2nd, 2010:
Hmm – maybe try working backwords…start with just link=”none” and does it work?
[Reply]
Sarthak Singhal reply on July 2nd, 2010:
Hi Sators,
I tried that way as well but still no luck.
New shortcode used: [gallery link="none" columns="4" orderby="rand"]
Sators reply on July 2nd, 2010:
no I mean try [gallery link="none"] and see if the link function works, then add in your other attributes individually to see which one is causing the problem.
Sarthak Singhal reply on July 2nd, 2010:
Even this is not working for me. It is still linking to the attachment page.
Sators reply on July 2nd, 2010:
Hmm – make sure you have replaced the correct line in the file
Sarthak Singhal reply on July 5th, 2010:
I do not know now what is the real issue, but it is just not working for me here.
July 19th, 2010 - 16:40
kick ass!! thank you for this. was looking around for hours. and it helped me solve this issue and one other. thanks again!
[Reply]
July 26th, 2010 - 09:23
Thank you. This works in WP 3.
[Reply]
August 3rd, 2010 - 23:08
A better way to do that is to overwrite the function gallery_shortcode() from media.php in your template functions.php file, like that you can always upgrade your WP.
My code : http://pastebin.com/Sg6AV8YJ
I add an other thing in this, the possibility to get the title of element.
[Reply]