Fix WordPress Private Post URL Redirect Loop

Also: An Alternative To Editing functions.php

Recently I put up a test post to test the functionality of this site after many updates and upgrades. After I made the post private, I noticed that hitting the URL would not gracefully redirect to an error page or the home page, but instead got stuck in an infinite redirect loop eventually giving an error about “redirected you too many times” or “this page isn’t redirecting properly”. It looks like this in Chrome:

This page isn't working www.bdragon.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

Or this in Firefox:

The page isn't redirecting properly
An error occurred during a connection to www.bdragon.com.
This problem can sometimes be caused by disabling or refusing to accept cookies.

Just in case this particular behavior bothers you, I can explain the steps I took to solve this issue. It turns out you need to remove an action on the “template_redirect” hook called “wp_old_slug_redirect” if you don’t want this endless redirect to occur.

Note that removing this action could have other consequences… I am assuming that old slugs will not redirect to new slugs after you do this. Many people don’t like this default redirect behavior anyway so it’s up to you to decide if you’re ok with removing it entirely.


What is an “old slug” and a “new slug” you ask? It is essentially a permalink URL for your WordPress post. An example would be a post you have published at the following URL:

http://example.com/old-slug

So if you then decided to change the slug, say to:

http://example.com/new-slug

The “wp_old_slug_redirect” action performs a redirect of “old-slug” to “new-slug” whenever anyone visits that old page URL. Therefore, removing this action via the method I describe below would disable this functionality – so be aware of this. The end-user should still get the generic WordPress “Not Found” error page though, with the whole theme, links, and a search etc. (see end screen shot below), if this helps you decide.


Firstly, these instructions apply to WordPress 5.6 which is the version that I used when implementing these changes. To remove the action wp_old_slug_redirect on the template_redirect hook, some said to just edit the “functions.php” file. I found that there are many reasons you should avoid doing this – for example if you switch your theme, any customizations made in your functions.php file are lost. Looking for an alternative, I saw that you can make your own simple custom “site plugin” to use locally on your own site and essentially implement the same end effect. I think this is also a great option if there are other “functions.php” modifications I would want to do later – they can just be done in the site plugin instead!


Creating your own (basic, empty) site plugin for WordPress is really simple. On my local system I created a directory named “bdragon-plugin” and created a file inside the directory called “bdragon-plugin.php” – so rename these as you’d prefer but keep the names the same for both! outside of the .php extension. Basically it looks like this in Windows File Explorer:

Edit the bdragon-plugin.php file in a text editor of your choice, paste in the following for the entire contents of the file – again, change the “bdragon.com” in the name and description to text of your choice:

<?php
/*
Plugin Name: bdragon.com Site Plugin
Description: Site-specific code changes for bdragon.com.
*/
/* Start Adding Functions Below this Line */



/* Stop Adding Functions Below this Line */
?>

Then you’re going to want to right-click on that main plugin folder (“bdragon-plugin”) and select Send to > Compressed (zipped) folder from the context menu.

Now go to your WordPress Admin area and navigate to Plugins > Add New. On this screen you should see a button to Upload Plugin near the “Add Plugins” title.

Use this Upload Plugin button to upload the zip file you just created (bdragon-plugin.zip), which should create a new site plugin on your site that you can now activate.

After uploading and activating the plugin you should now be able to navigate to Plugins > Plugin Editor, and click OK on the warning pop-up if you get one. Pick your newly added site plugin from the drop-down and click the “Select” button. This should bring up the contents of your plugin’s php file. In between the “Add Functions Below” and “Stop Adding Functions” line, you can add this line to stop the redirect loop:

remove_action('template_redirect', 'wp_old_slug_redirect');

The end result will look something like this:

Then just click “Update File” and the issue of the endless redirects should now be fixed! Instead of a redirect error, I now get the WordPress not found error page which looks like this: