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

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.