WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

In wordpress, an unattached attachment URL is


Once after it's attached to a parent, it goes with parent's URL structure, for example:


I need my attachments URL contains only root domain as the unattached ones. How to make it?

share|improve this question

migrated from stackoverflow.com Aug 5 '12 at 15:12

This question came from our site for professional and enthusiast programmers.

Just out of curiosity, why? – jprofitt Aug 5 '12 at 13:40
up vote 0 down vote accepted

You can add a filter to the function that gets attachment links to force that format:

function wpa60888_attachment_link( $link, $id ){
    return home_url() . '/?attachment_id=' . $id;
add_filter( 'attachment_link', 'wpa60888_attachment_link', 10, 2 );

Calls to get_attachment_link will have this filter applied to the output.

share|improve this answer
This filter works only if site is under default permalink structure. If site use pretty permalink structure, it will go with the pretty ones. – Jenny Aug 5 '12 at 21:35
Just figure out where I went wrong-- the file I add the above function was required if is_admin. Now I have the file loaded disregarding admin or not, the filter works. Thanks! – Jenny Aug 5 '12 at 22:04

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.