Stop WordPress from reserving slugs for media items?

I am experiencing some odd behavior, that apparently happens “out of the box” with WordPress.

If I upload a file through the media manager called: services.jpg; then go try to create a page with the permalink http://example.com/services/; The slug services-2 is given instead, because the attachment is already using that slug.

Visiting http://example.com/services/ loads the attachment page.

I have not enabled any plugins or added anything into functions.php to modify the rewrites for attachments.

Has anyone run into this before? Know where to start to disable this functionality?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Thank you for the response everyone. I played around with macemmek’s solution and I think it led me to an even better solution:

add_filter( 'wp_unique_post_slug_is_bad_attachment_slug', '__return_true' );

That is all that is needed. This will automatically ‘skip’ the default assigned slug on any attachment. So an attachment that might normally get the slug “services” will now get the slug “services-2”.

Method 2

You may hook wp_unique_post_slug() and append some string to the original slug if the post is an attachment type. The original slug based on post title will remain free.

UPDATED after Rachel Baker’s comment: original slug suffix is some random string. It does not guarantee uniqueness but may be enough for simple use cases.

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

Method 3

Tom’s comment is correct, in that this is normal behaviour and there is not much you can do to stop it.

But you can change slugs after things are uploaded/created. While you shouldn’t do this if the links are known/publicised/listed on search engines, if you’re just trying to fix this problem occasionally after an upload then you can edit the slug of the attachment (to be something like services-attachment), and then edit the slug of the page to be services.

To do this, go to Media, click Edit under the attachment you want to edit (or if on grid view, click the attachment then click Edit more details), then at the top you’ll see the permalink.. clicking Edit here will let you change the slug.

Then repeat the same process for the page itself.

Method 4

I think what you need to do here is use a redirect to prevent people from seeing the media items. It’s a good fit with the other solutions presented here. Prepend something to the media item slugs, the write a redirect to ‘get rid’ of the media pages.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x