Directories versus permalinks: How to prevent collisions in WordPress

If there are directories inside of the WordPress directory (popularly known as folders), identically worded permalinks don’t work, because the server checks their existence before it sends a request to WordPress.

This happens already with a default installation:

If we create a page with the permalink wp-admin/, that page will be unreachable. We should not change the order of server checks – existing files and directories over virtual – but we can prevent the creation of such permalinks.

There is a hook for that. Actually, even three. They have the beautiful names wp_unique_post_slug_is_bad_hierarchical_slug, wp_unique_post_slug_is_bad_flat_slug and wp_unique_post_slug_is_bad_attachment_slug. They are called when WordPress creates the slug for the permalink of a post or attachment.

We get two arguments on all three hooks: a boolean value (default: FALSE) and the slug. If we return TRUE and tell WordPress that this is a bad slug, it will search for another slug. During that, it adds ascending numbers to the original slug until it finds one that does not collide with other permalinks or our filter.
So our wp-admin/ would be changed to wp-admin-2/.

The filter function can be very simple:

The path has to be changed if WordPress is installed in a separate directory.

Because this code does not need a user interface and just works after enabling, we have put it into our repository Mini-Plugins on GitHub. You can download it here as a separate plugin: Protect directory URLs.

Post Sharing

Author Avatar

I am a developer and author for MarketPress. In my free time, I am a moderator for several WordPress communities.

Also Interesting

New Plugin: Slack Connector - Connect WordPress, WooCommerce and Slack

by Michael Firnkes

Initially we merely wanted to optimize our own Slack-processes. With automated notifications from our MarkettPress shop, the blog and our forums. The resul ...

Read more

Inpsyde is the first VIP partner in Germany

by Michael Firnkes

Automattic has given us the title of VIP Service Partner, the first in Germany, Austria and Switzerland. Worldwide, there are only eleven com ...

Read more

Inpsyde is a WooExperts Gold Partner of WooCommerce

by Michael Firnkes

A few weeks ago, Inpsyde GmbH with MarketPress became a certified WooExperts Gold Partner. This is confirmation from WooThemes of our agency’s know-h ...

Read more

BackWPup Pro: Secure WordPress Backup with Google Drive

by Michael Firnkes

The Pro version of our BackWPup plugin supports the backup of WordPress databases and files to Google Drive. But how do you set something like that up? And ...

Read more