WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to link each gallery thumbnail if i use the built in WordPress native gallery?

share|improve this question

You can filter 'post_gallery'. If you return not an empty string WordPress will use your return value and not build the gallery with the native code. But then you have to rebuild the whole gallery code …

The second option: Mask the gallery shortcode and filter just 'wp_get_attachment_link'.

Sample code (not tested, just an idea):

add_action( 'after_setup_theme', 'wpse_53368_replace_gallery_shortcode' );

 * Replace the default shortcode handlers.
 * @return void
function wpse_53368_replace_gallery_shortcode()
    remove_shortcode( 'gallery' );
    add_shortcode( 'gallery', 'wpse_53368_gallery_shortcode' );

function wpse_53368_gallery_shortcode( $attr )
    // Add a filter for attachment links:
    add_filter( 'wp_get_attachment_link', wpse_53368_gallery_link_filter, 10, 6 );

    // Let WordPress create the regular gallery …
    $gallery = gallery_shortcode( $attr );

    // Remove the filter for attachment links:
    remove_filter( 'wp_get_attachment_link', wpse_53368_gallery_link_filter, 10 );

    return $gallery;
function wpse_53368_gallery_link_filter( $full_link, $id, $size, $permalink, $icon, $text )
    // Inspect the attachment by its ID and build a link.
    return $link;

See How can I add a URL field to the attachments window? for an example how to store another URL in the attachment's meta data.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.