No Slug Conflicts with Trash

No Slug Conflicts with Trash

Wordpress plugin

Install on Wordpress

App Details

NOTE: WordPress 4.5 incorporated the functionality of this plugin and thus it is no longer needed unless you’re still running an older version of WP.

This plugin implements the belief that a trashed page or post should not in any way conflict with a new page or post when it comes to slugs. In essence, a new page or post should take precedence over anything in the trash. After all, the page/post is in the trash for a reason.

By default, WordPress takes into consideration posts and pages that have been trashed when deciding if the slug for a new post is already in use. Obviously, in general, WordPress should not allow duplicate slugs because that could interfere with permalinks. The thinking behind WordPress’s handling of the situation is that trashed posts/pages are still technically present, just inaccessible. It is possible that an author or admin would choose to restore a post from the trash, which WordPress feels should then occupy that same permalink as before it was trashed.

If what WordPress does is unclear, here’s an example to help clarify things:

  • WordPress ships with a page called “About” with the slug of “about”. The page’s URL would be http://yoursite/about/
  • Let’s say you trash that page and start a new page with the name “About”.
  • Due to a trashed page having the slug that would normally have been assigned to the new page (“about”), the new page gets the slug of “about-2”, resulting in the page’s URL being http://yoursite/about-2/

With this plugin, for this example, the new “About” page would get the slug “about” as one would hope.

That said, the plugin tries its best to restore untrashed posts to their original slug. The only time it fails to do so is if a new page or post has claimed the trashed post’s original slug, in which case the untrashed post is assigned a new slug.

See other sections of the documentation for more insight into the plugin’s functionality. See WP core ticket #11863 for discussion on the matter.

Links: Plugin Homepage | Plugin Directory Page | Author Homepage

The Solution Explained

An overview of the approach employed by the plugin to resolve the issue of slugs potentially conflicting with posts in the trash.

What WordPress does

In order to understand the crux of the implemented solution, a quick refresher on unique slug handling by WP:

Before published use, a desired slug is passed to wp_unique_post_slug() and a safe slug is returned. The safe slug may differ from the desired slug if any existing post (including a trashed post) has that slug (or, less likely, is invalid for permalink reasons such as feeds or date archives).

In order to prevent a trashed post’s slug from conflicting with a new post, this plugin takes an approach that is comprised of two primary tasks:

1. Permit a post to use a slug that conflicts with a trashed post.

The plugin hooks the ‘wp_unique_post_slug’ filter. If the desired slug matches the safe slug, the desired slug is unique and nothing needs to be done.

When the two slugs don’t match, it has to determine if the conflict is due to a trashed post or not, so it attempts to find a trashed post with the desired slug.

  • If no such trashed post is found, the conflict is with a live post (or was an otherwise unsuitable slug) so the safe slug must be used by the new post (so, again, nothing needs to be done).
  • If such a trashed post is found, then the plugin simply inverses the traditional behavior of wp_unique_post_slug(): give the trashed post the safe slug and return the desired slug for use by the new post (rather than having the trashed post retain its slug and forcing the new post to use the safe slug). In order to be able to restore a trashed post to its original slug, the trashed post’s original slug is stored in postmeta.

2. Restore a trashed post to its original slug, if necessary and possible.

For a post transitioning away from the ‘trash’ post status, check to see if its slug was rewritten (denoted by the presence of the postmeta field only assigned if the original slug was changed due to a conflict while the post was in the trash).

If there was no original slug, the slug was never changed and nothing more needs to happen.

When there is an original slug, check to see if that slug is still in use by a non-trashed post.

  • If not, restore the post slug to the original value.
  • If so, there is no recourse but to leave the post slug as-is (it is guaranteed to be unique already).

In either case, the postmeta field for the original slug value gets deleted since the now untrashed post is exposed in some fashion with its current slug and should abide by it going forward.

Pricing

Starting from $0 per month.

Check Out the Flash Cards Widget

By Common Ninja

Flash CardsTry For Free!

App Info

Rating

Reviewers

2 reviews

Tags

coffee2code
post
post_name
slug
trash

Developed By

Scott Reilly

Quick & Easy

Find the Best Wordpress plugins for you

Common Ninja has a large selection of powerful Wordpress plugins that are easy to use, fully customizable, mobile-friendly and rich with features — so be sure to check them out!

Testimonial

Testimonial plugins for Wordpress

Galleries

Galleries plugins for Wordpress

SEO

SEO plugins for Wordpress

Contact Form

Contact Form plugins for Wordpress

Forms

Forms plugins for Wordpress

Social Feeds

Social Feeds plugins for Wordpress

Social Sharing

Social Sharing plugins for Wordpress

Events Calendar

Events Calendar plugins for Wordpress

Sliders

Sliders plugins for Wordpress

Analytics

Analytics plugins for Wordpress

Reviews

Reviews plugins for Wordpress

Comments

Comments plugins for Wordpress

Portfolio

Portfolio plugins for Wordpress

Maps

Maps plugins for Wordpress

Security

Security plugins for Wordpress

Translation

Translation plugins for Wordpress

Ads

Ads plugins for Wordpress

Video Player

Video Player plugins for Wordpress

Music Player

Music Player plugins for Wordpress

Backup

Backup plugins for Wordpress

Privacy

Privacy plugins for Wordpress

Optimize

Optimize plugins for Wordpress

Chat

Chat plugins for Wordpress

Countdown

Countdown plugins for Wordpress

Email Marketing

Email Marketing plugins for Wordpress

Tabs

Tabs plugins for Wordpress

Membership

Membership plugins for Wordpress

popup

popup plugins for Wordpress

SiteMap

SiteMap plugins for Wordpress

Payment

Payment plugins for Wordpress

Coming Soon

Coming Soon plugins for Wordpress

Ecommerce

Ecommerce plugins for Wordpress

Customer Support

Customer Support plugins for Wordpress

Inventory

Inventory plugins for Wordpress

Video Player

Video Player plugins for Wordpress

Testimonials

Testimonials plugins for Wordpress

Tabs

Tabs plugins for Wordpress

Social Sharing

Social Sharing plugins for Wordpress

Social Feeds

Social Feeds plugins for Wordpress

Slider

Slider plugins for Wordpress

Reviews

Reviews plugins for Wordpress

Portfolio

Portfolio plugins for Wordpress

Membership

Membership plugins for Wordpress

Forms

Forms plugins for Wordpress

Events Calendar

Events Calendar plugins for Wordpress

Contact

Contact plugins for Wordpress

Comments

Comments plugins for Wordpress

Analytics

Analytics plugins for Wordpress

Common Ninja Apps

Some of the best Common Ninja plugins for Wordpress

Browse our extensive collection of compatible plugins, and easily embed them on any website, blog, online store, e-commerce platform, or site builder.

Flash Cards for Wordpress logo

Flash Cards

Create interactive flash cards that present questions and answers in a clear, customizable format to support learning, training, and user engagement.

Scroll Progress for Wordpress logo

Scroll Progress

Use a scroll progress bar that visually tracks reading on the page, improves navigation, and keeps visitors aware of their position.

Facebook Reviews for Wordpress logo

Facebook Reviews

Show Facebook reviews to build trust, improve credibility, and help visitors make confident purchase decisions that support higher sales.

Back to Top Button for Wordpress logo

Back to Top Button

Back to top button that improves navigation, speeds up browsing, and helps visitors move smoothly through long pages for a better user experience.

March Madness Bracket Maker for Wordpress logo

March Madness Bracket Maker

Create interactive March Madness brackets where users can make predictions, track tournament progress, and compete with others throughout every round.

Telegram Chat for Wordpress logo

Telegram Chat

Add Telegram Chat to your site to communicate with visitors, deliver instant support, and provide a smoother, more reliable user experience.

Consent Form for Wordpress logo

Consent Form

Create consent forms that collect signatures, save submissions, send notifications, and help you manage approvals efficiently.

Restaurant Menu List for Wordpress logo

Restaurant Menu List

Create a clear restaurant menu list that helps visitors explore dishes easily, understand key details, and make confident ordering decisions that support conversions.

Multi-Row Image Slider for Wordpress logo

Multi-Row Image Slider

Showcase visuals with a multi row image slider that displays images in layered rows, improves design, and helps visitors explore content more easily.

What's New Popup for Wordpress logo

What's New Popup

Show product updates, new features, and announcements in a What's New popup that keeps users informed and engaged.

World Clock for Wordpress logo

World Clock

Display multiple time zones with a world clock widget that offers clear global times, customizable styles, and a responsive design for better user experience.

RSVP Form for Wordpress logo

RSVP Form

Collect event responses with an RSVP form that lets guests register easily, saves submissions, sends notifications, and helps you organize attendance efficiently.

Discover Apps By Platform

Discover the best apps for your website

WordPress
Wix
Shopify
Weebly
Webflow
Joomla
PrestaShop
Shift4Shop
WebsiteX5
MODX
Opencart
NopCommerce

Common Ninja Search Engine

The Common Ninja Search Engine platform helps website builders find the best site widgets, apps, plugins, tools, add-ons, and extensions! Compatible with all major website building platforms - big or small - and updated regularly, our Search Engine tool provides you with the business tools your site needs!

Multiple platforms