{"id":584,"date":"2021-02-04T18:45:27","date_gmt":"2021-02-05T01:45:27","guid":{"rendered":"https:\/\/www.bdragon.com\/?p=584"},"modified":"2021-02-04T18:45:32","modified_gmt":"2021-02-05T01:45:32","slug":"fix-wordpress-private-post-url-redirect-loop","status":"publish","type":"post","link":"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/","title":{"rendered":"Fix WordPress Private Post URL Redirect Loop"},"content":{"rendered":"<div class=\"entry\">\n\n\n<h3 class=\"wp-block-heading\">Also: An Alternative To Editing functions.php<\/h3>\n\n\n\n<p>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 &#8220;redirected you too many times&#8221; or &#8220;this page isn&#8217;t redirecting properly&#8221;.  It looks like this in Chrome:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"202\" data-attachment-id=\"587\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-chrome\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-chrome.png\" data-orig-size=\"512,202\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-chrome\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-chrome.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-chrome.png\" alt=\"This page isn't working www.bdragon.com redirected you too many times.\nTry clearing your cookies.\nERR_TOO_MANY_REDIRECTS\n\" class=\"wp-image-587\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-chrome.png 512w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-chrome-300x118.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>Or this in Firefox:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"252\" data-attachment-id=\"594\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-firefox\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-firefox.png\" data-orig-size=\"739,252\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-firefox\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-firefox.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-firefox.png\" alt=\"The page isn't redirecting properly\nAn error occurred during a connection to www.bdragon.com.\nThis problem can sometimes be caused by disabling or refusing to accept cookies.\n\" class=\"wp-image-594\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-firefox.png 739w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-firefox-300x102.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n\n<p>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 &#8220;template_redirect&#8221; hook called &#8220;wp_old_slug_redirect&#8221; if you don&#8217;t want this endless redirect to occur.<\/p>\n\n\n\n<p>Note that removing this action could have other consequences&#8230; I am assuming that old slugs will not redirect to new slugs after you do this.  Many people don&#8217;t like this default redirect behavior anyway so it&#8217;s up to you to decide if you&#8217;re ok with removing it entirely.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<p>What is an &#8220;old slug&#8221; and a &#8220;new slug&#8221; 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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/example.com\/old-slug<\/pre>\n\n\n\n<p>So if you then decided to change the slug, say to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/example.com\/new-slug<\/pre>\n\n\n\n<p>The &#8220;wp_old_slug_redirect&#8221; action performs a redirect of &#8220;old-slug&#8221; to &#8220;new-slug&#8221; whenever anyone visits that old page URL.  Therefore, removing this action via the method I describe below would disable this functionality &#8211; so be aware of this.  The end-user should still get the generic WordPress &#8220;Not Found&#8221; error page though, with the whole theme, links, and a search etc. (see end screen shot below), if this helps you decide.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<p>Firstly, these instructions apply to <em>WordPress 5.6<\/em> which is the version that I used when implementing these changes.  To remove the action <em>wp_old_slug_redirect<\/em> on the <em>template_redirect<\/em> hook, some said to just edit the &#8220;functions.php&#8221; file.  I found that there are many reasons you should avoid doing this &#8211; 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 &#8220;site plugin&#8221; 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 &#8220;functions.php&#8221; modifications I would want to do later &#8211; they can just be done in the site plugin instead!<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<p>Creating your own (basic, empty) site plugin for WordPress is really simple.  On my local system I created a directory named &#8220;bdragon-plugin&#8221; and created a file inside the directory called &#8220;bdragon-plugin.php&#8221; &#8211; so rename these as you&#8217;d prefer but keep the names the same for both! outside of the .php extension.  Basically it looks like this in Windows File Explorer:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"205\" data-attachment-id=\"603\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-plugin-folder\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-folder.png\" data-orig-size=\"642,205\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-plugin-folder\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-folder.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-folder.png\" alt=\"\" class=\"wp-image-603\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-folder.png 642w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-folder-300x96.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<p>Edit the bdragon-plugin.php file in a text editor of your choice, paste in the following for the entire contents of the file &#8211; again, change the &#8220;bdragon.com&#8221; in the name and description to text of your choice:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/*\nPlugin Name: bdragon.com Site Plugin\nDescription: Site-specific code changes for bdragon.com.\n*\/\n\/* Start Adding Functions Below this Line *\/\n\n\n\n\/* Stop Adding Functions Below this Line *\/\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>Then you&#8217;re going to want to right-click on that main plugin folder (&#8220;bdragon-plugin&#8221;) and select Send to &gt; Compressed (zipped) folder from the context menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"291\" data-attachment-id=\"605\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-sendto\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-sendto.png\" data-orig-size=\"632,291\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-sendto\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-sendto.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-sendto.png\" alt=\"\" class=\"wp-image-605\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-sendto.png 632w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-sendto-300x138.png 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/figure>\n\n\n\n<p>Now go to your WordPress Admin area and navigate to Plugins &gt; Add New.  On this screen you should see a button to Upload Plugin near the &#8220;Add Plugins&#8221; title.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"261\" data-attachment-id=\"608\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-upload-plugin\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-upload-plugin.png\" data-orig-size=\"730,261\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-upload-plugin\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-upload-plugin.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-upload-plugin.png\" alt=\"\" class=\"wp-image-608\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-upload-plugin.png 730w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-upload-plugin-300x107.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/figure>\n\n\n\n<p>Use this Upload Plugin button to upload the zip file you just created (bdragon-plugin.zip), which <em>should<\/em> create a new site plugin on your site that you can now activate.<\/p>\n\n\n\n<p>After uploading and activating the plugin you should now be able to navigate to Plugins &gt; 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 &#8220;Select&#8221; button.  This should bring up the contents of your plugin&#8217;s php file.  In between the &#8220;Add Functions Below&#8221; and &#8220;Stop Adding Functions&#8221; line, you can add this line to stop the redirect loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action('template_redirect', 'wp_old_slug_redirect');<\/code><\/pre>\n\n\n\n<p>The end result will look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"379\" data-attachment-id=\"612\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-plugin-editor\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-editor.png\" data-orig-size=\"627,379\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-plugin-editor\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-editor.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-editor.png\" alt=\"\" class=\"wp-image-612\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-editor.png 627w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-plugin-editor-300x181.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure>\n\n\n\n<p>Then just click &#8220;Update File&#8221; 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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large img-drop-shadow\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"415\" data-attachment-id=\"622\" data-permalink=\"https:\/\/bdragon.com\/lair\/2021\/02\/fix-wordpress-private-post-url-redirect-loop\/ss-404\/\" data-orig-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-404.png\" data-orig-size=\"730,415\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ss-404\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-404.png\" src=\"https:\/\/new.bdragon.com\/lair\/wp-content\/uploads\/ss-404.png\" alt=\"\" class=\"wp-image-622\" srcset=\"https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-404.png 730w, https:\/\/bdragon.com\/lair\/wp-content\/uploads\/ss-404-300x171.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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 &#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[177,43],"tags":[186,184,185,191,190,181,180,179,183,188,178,189],"class_list":{"0":"post-584","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-techie-things","7":"category-web-stuff","8":"tag-action","9":"tag-fix","10":"tag-hook","11":"tag-how-do-i","12":"tag-how-to","13":"tag-post","14":"tag-private","15":"tag-redirects","16":"tag-site-plugin","17":"tag-slugs","18":"tag-wordpress","19":"tag-wordpress-5-6","20":"anons"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/peSL2o-9q","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":0,"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"wp:attachment":[{"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bdragon.com\/lair\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}