WordPress Gallery Shortcode with No Image Link

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:

UPDATE – No more changing WordPress Core Files! Let’s get smarter here. This is now a PLUGIN!  Install by going to Plugins > Add New > search for “Gallery with No Image Links”

Whala – now you can use gallery link=”none” and the images not be forced linked to something.

Find this useful? Take just a moment and give a $1. Thanks!
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
  • deschamps_24

    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]

  • Pingback: WordPress Galleries | WebSiteCleanup.com

  • http://fontsite.com joyce lukaczer

    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]

  • http://sexyteim.com sho

    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]

  • Olivier

    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]

  • Ana MB

    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]

  • http://www.sarthaksinghal.com Sarthak Singhal

    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.

  • http://terra45.com CMB

    kick ass!! thank you for this. was looking around for hours. and it helped me solve this issue and one other. thanks again!

    [Reply]

  • Pingback: WordPress Gallery Shortcode with No Image Link » The Nowhere News

  • padraic

    Thank you. This works in WP 3.

    [Reply]

  • Merimac

    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]

  • Pepe

    Brilliant!!

    [Reply]

  • http://www.housekeepingtoys.com/toys/grills toy grills

    Thats some great fundamentals there, already know some of that, but you can always learn more. I doubt a “kid” could put together such information as dolphin278 suggested. Maybe he’s just trying to be “controversial? lol

    [Reply]

  • http://twitter.com/demo38 Jonathon Harris

    Thank you for this code snippet, by adding this into the second function block of code I found here: http://geekeemedia.com/wordpress/add-a-custom-url-to-your-wordpress-gallery-images/ I was able to add link=”none” to the WP gallery shortcode in order to override the WP gallery links – without hacking core files. Great stuff!

    [Reply]

    Sators reply on January 12th, 2011:

    Awesome! Can you paste back here your completed merged block of code?

    [Reply]

    Torndownunit reply on February 1st, 2011:

    Did you ever find the solution to the column problem? I am having the same issue. I don’t have enough technical knowledge about WordPress to fix it. Thanks,

    [Reply]

    Jonathon Harris reply on February 1st, 2011:

    Not sure what to suggest, I used it in a custom theme with 5 columns and it dropped right in. So, your gallery worked with the correct number of columns BEFORE trying to add the new codeblock to your functions.php file?

    Torndownunit reply on February 2nd, 2011:

    Ya no problems before adding the code. After adding, the images didn’t have links which is great. But the gallery has the same issue magicsister mentioned in the first post. There is one long column of images. You can see it at the bottom of the pagere here: http://purewatercanada.com/wordpress/?page_id=10

    Torndownunit reply on February 2nd, 2011:

    p.s. as magicsister said, the column # selector in the gallery settings just doesn’t seem to work once the code is applied. I am assuming it’s likely something fairly simple in the code since everything else works with it. I just don’t know enough about code to figure it out. Thanks for any tips!

    Sators reply on February 2nd, 2011:

    OK – sorry about that folks. WordPress was taking the HTML I was placing in the post and actually parsing it out as HTML…not as the code block I was trying to display. So tags and were being interpreted as real html, not text.

    Try again!

    Jonathon Harris reply on February 2nd, 2011:

    That would be the culprit!

    Torndownunit reply on February 2nd, 2011:

    Perfect! Works great now. Thanks a ton!

  • http://twitter.com/demo38 Jonathon Harris

    Thanks again ;)

    [Reply]

  • magicsister

    it works if you want to get away the link and I was so happy about it, but I tried it now with 2 themes and everwhere it is the same problem: every picture is shown in one column, the function: columns=”4″ doesn’t work anymore. I tried so much now but I can’t help myself :(

    [Reply]

    Sators reply on February 2nd, 2011:

    Try again my friend – recopy the code. WordPress was stripping out certain HTML blocks….

    [Reply]

  • http://twitter.com/geoffmuskett Geoff Muskett

    Fantastic! Works great, surely this should come with WordPress’ standard package. Anyway Sators, thanks for posting this great piece of code!

    [Reply]

  • http://sunny-music.ru reggi

    Awesome! It’s realy best function when i see at last time. I really help me, now I can do many change with this gallery.

    [Reply]

  • http://profiles.google.com/edpittol Eduardo Pittol

    To reuse the code from the WP I got out the original function (gallery_shortcode) and applied a filter on it if link = “none”.

    add_shortcode( ‘gallery’, ‘new_gallery_shortcode’ );

    /**
    * The Gallery shortcode – modified for link=”none”.
    */
    function new_gallery_shortcode($attr) {
    global $post, $wp_locale;

    $output = gallery_shortcode($attr);

    // remove link
    if($attr['link'] == “none”) {
    $output = preg_replace(array(‘/]*>/’, ‘//’), ”, $output);
    }

    return $output;

    }

    [Reply]

    Benedikt Nuetzel reply on June 18th, 2011:

    thx eduardo, this little snippet just works!

    [Reply]

    Susan Thembi reply on August 18th, 2011:

    Thanks for writing this up Sators and now Eduardo for making this simpler. 

    [Reply]

    bitkahuna reply on December 13th, 2011:

    you rock. thanks!

    [Reply]

  • Pingback: Eduardo Pittol» Posts » Wordpress: remover link das imagens na galeria de fotos

  • Andrew

    Hey, this really helped me out! Thanks.

    [Reply]

  • http://www.facebook.com/patricktang Patrick Tang

    You are the man, Eduardo!

    Note: Just to make sure that you don’t copy "" at the end to your theme function file.

    [Reply]

  • http://www.facebook.com/rvelie Regina Velie

    Wow!  Thank you so much. Amazing. 

    [Reply]

  • http://www.thecomplexmedia.com/blog/ theComplex

    wow, this worked! Thank you so much!!!

    [Reply]

  • http://www.jessemcfarlane.com Jesse McFarlane

    Worth making into plugin form instead of relying on functions.php
    http://wpcandy.com/teaches/how-to-create-a-functionality-plugin

    [Reply]

  • Pingback: Galerija brez presledkov in brez levega klika - Iva Novak Photography

  • http://profiles.google.com/cgwamba Craig Gwamba

    Thanks.  This really helped.  Wakapenga

    [Reply]

  • http://twitter.com/JSayTweet JJ Sereday

    I am receiving this warning on the backend in wordpress

    Any ideas?Warning: Cannot modify header information – headers already sent by (output started at /nfs/c08/h01/mnt/126646/domains/trendyslave.com/html/wp-content/themes/TheStyle/functions.php:164) in /nfs/c08/h01/mnt/126646/domains/trendyslave.com/html/wp-includes/pluggable.php on line866

    [Reply]

    Sators reply on January 4th, 2012:

    This means you likely have a PHP error somewhere.  Check your PHP Error log to see if that shows anything…

    [Reply]

  • http://blog.mattsatorius.com/ Sators

    Finally got around to creating this as a WordPress Plugin – You can download it here: http://blog.mattsatorius.com/wp-content/uploads/2010/04/gallery-nolink.zip

    [Reply]

  • Look

    Awesome!!

    [Reply]

  • Rusty Talent

    Thanks for the plug-in!

    [Reply]

  • Fidel

    wow!!! there’s a plugin for everything it seems. Kinda cool when you spend 10 hours trying to figure something out. 

    [Reply]

  • thunderkat

    THANK YOU…. :-)

    [Reply]

  • Lucas Balzer

    How about a link with no images? This code generates just a text link to launch a lightbox gallery. Usage:

    [gallery images="none"]HTML THAT WILL BE YOUR LINK[/gallery]

    add this to your functions.php:

    add_shortcode( ‘gallery’, ‘new_gallery_shortcode’ );

    function new_gallery_shortcode($attr,$content) {
    global $post, $wp_locale;
    $output = gallery_shortcode($attr);
    // remove images
    if($attr['images'] == “none”) {
    $output = preg_replace(array(‘/]*>/’, ‘/]*>/’, ‘/]*>/’,”/<a/"), array('','','',"<a rel="prettyPhoto[slides]""), $output);

    // this function adds prettyPhoto's API launch call to the wp_footer
    function your_function() {
    echo '
    $(function () {$(“.launch-slides”).click(function (){$.prettyPhoto.open($(“a[rel^='prettyPhoto']“).map(function(){return $(this).attr(“href”);})); return false;});});
    ‘;

    }
    add_action(‘wp_footer’, ‘your_function’, 100);
    return ‘‘.$content.”“.$output;
    }
    return $output;
    }

    add this to footer.php (this is the prettyPhoto plugin initializer — different from the API launch command)

    $().prettyPhoto({YOUR PARAMETERS});

    Note: you must also install prettyPhoto (www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/) for this particular snippet to work. If you’re using another lightbox, you’ll

    [Reply]

  • Lucas Balzer

    sorry… the //remove images lines should read:

    // remove images
    if($attr['images'] == “none”) {
    $output = preg_replace(array(‘/]*>/’, ‘/]*>/’, ‘/]*>/’,'/<a/'), array('','','','<a rel="prettyPhoto[slides]"'), $output);

    [Reply]

  • George Pau

    great plugin. thanks. stil… how can I remove the hover off the images ?

    [Reply]

  • Hilmon

    I just commented out the if statement to automatically remove all links from all galleries …. very handy (for the particular project I’m working on) … Thanks :-)

    [Reply]

  • http://www.wordimpressed.com Devin Walker

    The plugin works like a charm!

    [Reply]

  • lulu

    Thanks, the plugin is great! Is there a way to have only some of the images have no link and have the others link?

    [Reply]

  • ertan hekimler

    thanxxx!!

    [Reply]