I have a problem understanding the behavior of the Attachment Page Permalinks Into Setting-->Permalinks I have set http://www.example.com/sample-post/ as the preferred setting.

When I upload an image a permalink for the Attachment Page is automatically created. Example:

  • I'm into Service (a page) and I upload landscape.jpg to the media gallery.
  • The permalink http://www.example.com/services/landscape/ is automatically created
  • If I want to create a page at named landscape with services as parent I can't
  • When i create the page Landscape wordpress rename it example.com/services/landscape-2/

Is there a way to modify the way WordPress handles the permalinks of Attachment Pages that are automatically created when we upload the images to the media gallery?

  • Can we modify all the Attachment Page having into the permalink something like /img-upload/ so wordpress will not get confused ?
  • Is it possiible to revert to the old style permalink handling just for the attachment pages? Before with the permalink of the attachment page having this kind of url example.com/?attachment_id=37 we had no this problem.. and for the seo we were just redirecting all the attachment page url to the page that was containing the image; solving in this way the duplicated content issue that was giving the creation of a page with Title description and image...

Thanks in advance!

2 Answers 2


To provide an answer to your first question and for future reference to tackle the problem you described with the slug already being in use when you first uploaded an attachment to the same parent. The following code will get rid of this 'annoying' thing (annoying in some use cases).

This code rewrites the slug of the attachment upon uploading to add a prefix (or suffix) to the slug. In that case you have less chance of getting in the way of page slugs in the future.

add_action('add_attachment', function($postId) {
    // get the attachment post object
    $attachment = get_post($postId);
    // get the slug for the attachment
    $slug = $attachment->post_name;
    // update the post data of the attachment with an edited slug
        'ID' => $postId,
        'post_name' => 'media-'.$slug, //adds a prefix
        //'post_name' => $slug.'-photo', //adds a suffix

I hope this will help anyone.

  • Will this work if I use a named function ('mysite_update_attachment_slug') and add this function to the action? add_action( 'add_attachment', 'mysite_update_attachment_slug' ); function mysite_update_attachment_slug($postId) { ... } Or does it need to be an anonymous function due to the media manager javascript? (and does the passed variable name need to be $postId ?)
    – nimmolo
    Commented Jun 6, 2021 at 21:55
  • 1
    You can make it a named function as well of course. This is all PHP and goes in your functions.php so it is not used for the JavaScript side. You can rename the variable $postId to something you like, but make sure to rename it throughout the whole function.
    – leendertvb
    Commented Jun 7, 2021 at 6:50

The "attachment page" is just a post type, like "page" or "post" are.

The slug in the URL for each post must be unique, shared slug is only permitted accross different post types (since WordPress 4.1), but with some limitations.

One obvious limitation is that same slug can not be allowed for posts which have the same post parent. If the post parent is the same, the URL path will be the same and the post slug will be in the same level in that path, which is obviously a problem: same path and slug would result in the same URL for the two different contents. That is why WordPress adds -2 to one of the them and fix this issue.

The situation described above is what is happening to you. You are trying to create two posts of different post types that share post parent and URL path.

Even you use a different URL structure for pages and attachments, WordPress won't allow same slug in both if they share the same parent post.

So the best solution is to give each post, attachment and page, different names and slugs. URL manipulation is quite more complicated and won't give you the desired SEO benefits you think it will give you.

That is my opinion from a SEO perspective. Using different slugs, for example example.com/services/landscape-photo/ for attachement page, won't hurt your SEO at all.

In the other hand, redirecting the attachment page, as you are doing, may actually hurt your SEO. I assume you are doing it with 3xx status code which means "Content moved for xx reason". For example, 301 code means "Content moved permanently to a new location".

If you use 3xx status code to redirect attachment page to parent page, you are doing it wrong. Simply because you have not moved the content, the content of the URL where you are redirecting to is not the same content of the URL where you are redirecting from, so there is not duplicated content and there is not moved content. That is why the redirection is incorrect from a SEO perspective.

If you don't want attachement pages being indexed by search engines, I would use robots meta tag with noindex value. Other methods can be used to avoid attachment be indexed and avoid fake redirections, it depends on the exact situation.

  • Hi cybmeta thanks for the answer! The problem is that while you are adding image that will work as a a href for another page, a future child of the page where the image has been uploaded, you have to remember to change the name of the image... If we could have a custom way to create ( into the core) the permalink for the attachment page i.e. example.com/upload-imgs/attachment-page-filename/ we could have 0 interference while creating new page with the same name of the file uploading in the parent page. Do you know any way to accomplish this?
    – William
    Commented Jul 29, 2016 at 9:00
  • Sorry but I think that is your very specific needs and what you think is the solution. My answer is, basically, "I think your are wrong about the problem description and about what you think the solution (answer) should be". Sorry if I don't give you the answer you wish. In the linked trac ticket there is a broad discussion of how the core should hanlde attachment URLs in the context of shared slugs, parent posts and paths. I recommend you to read it. If you want to propose anohter way to be included in core, you can always create a new ticket.
    – cybmeta
    Commented Jul 29, 2016 at 9:47
  • By the way, example.com/upload-imgs/attachment-page-filename/ implies thea example.com/upload-imgs/ URL, which tipically should be the post type archive, in this case "attachment" post type archive. By default, attachement post type is registered without support for archives.
    – cybmeta
    Commented Jul 29, 2016 at 9:50
  • *"Even you use a different URL structure for pages and attachments, WordPress won't allow same slug in both if they share the same parent post" For this you were remembering me that I was wrong about the problem description! :) Thanks for that! Is there a way to have the attachment permalink page with this structure example.com/?attachment_id=37 ( not trying in any way to assign a name to the page just the id number or something similar to this structure?) and all the other permalinks (page or post) with the example.com/sample-post/ structure ?
    – William
    Commented Jul 29, 2016 at 22:52

Your Answer

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

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