How to Ensure Unique URLs for WordPress Image Attachments and Posts

Hostinger Sale
How to Ensure Unique URLs for WordPress Image Attachments and Posts

When you upload a media file to WordPress either via the post editor, or via the “Media” tab on the left hand side of the WordPress dashboard, it creates two URLs. The first of course is the URL for the media file itself. This is the one used in your post if it’s an image between the <img> tags of your content. Opening this URL in a web browser will display just the image itself and nothing else.

Looking at the Media “Attachment URL”

The second URL however, is known as an “attachment” URL. This points to an actual page on your blog that holds the media as the sole content, with the file name as the title. Many bloggers have been puzzled by this behavior for two reasons. First, it seems completely unnecessary to have a second URL dedicated to just the image or video. And second, it’s done almost without your knowledge. If you upload an image from the WordPress text editor, you never even see the additional URL being created.

So why does WordPress have these “attachment” URLs at all? One reason I can think of is if you want to share the media with someone, you might want to send a URL that has your blog title and other links so that they can view other content as well, instead of just an image file.

Conflict Between Attachment URL and Post URL

Because the attachment URL is created in the background, it’s possible for you to accidentally create a post URL that matches a previous attachment URL and which makes the former invalid! For example, here I have an image file called “Kingfisher” that I’ve uploaded to my test blog. You can view the attachment URL when I go to “Edit Image Details”:

As you can see, it’s created the following URL for the image attachment:

This is different from the file URL itself which is organized under date folders like the one below:

When we view the URL of the image attachment in the browser, we get this:

As you can see, it’s just a regular WordPress page.

But what if I create a blog post with the title “Kingfisher”, with a matching URL, like this?

My new blog post here shares the exact same URL as the old image attachment we saw in the previous screenshots. And this “overwrites” the old attachment URL:

The previous URL is now inaccessible. This conflict of URL can show up in unexpected ways. The last thing you want is to accidentally have an attachment page show up instead of your blog post!

Adding Randomization to the Attachment Page URLs

One solution is to randomize the attachment URL a little bit so as to drastically reduce the chances of there being a URL clash between your blog post and the media page. Of course, this isn’t foolproof if you deliberately name a post with the same randomization characteristics as your attachment URL. But in regular operations, it should be enough. Append the following code into your functions.php file, or wherever you place custom PHP code:

function append_attachment_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
  if ( 'attachment' == $post_type )
    $slug = $original_slug . uniqid( '-' );
  return $slug;
add_filter( 'wp_unique_post_slug', 'append_attachment_slug', 10, 6 );

If you don’t know how to add code to WordPress like this, check out my earlier tutorial on the same.

What we’re doing here is intercepting the function that generates unique URL slugs, and checking whether or not it’s an attachment URL. If it is, we append a bit of randomness to it using the “uniqid” function which should be good enough for our purposes. Save your changes.

Now when we upload the same image file called “Kingfisher”, we get the following attachment URL:

Unique URLs for WordPress Image Attachments

As you can see, there’s now a random bunch of characters added to the URL that makes it all but impossible for two of them to have the same slug. Certainly you would never have such a slug for one of your posts, because it would make for terrible SEO!

This trick allows us to pretty much guarantee that attachment pages and posts will never share the same URL, thereby sparing us potential conflicts and headaches.

See also: WordPress Hosting Reviews

One Reply to “How to Ensure Unique URLs for WordPress Image Attachments and Posts”!

  1. Well explained post and important as well because having unique urls not only for wordpress image attachments is very crucial aspect of On-Page Seo techniques.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.