Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#41036 closed defect (bug) (invalid)

wp_redirect fails with error in WP 4.8

Reported by: dkurth's profile dkurth Owned by:
Milestone: Priority: normal
Severity: major Version: 4.8
Component: Administration Keywords: reporter-feedback needs-patch close
Focuses: Cc:

Description

After I allowed the update to 4.8. The call wp_safe_redirect and wp_redirect both fail.

Change History (9)

#1 follow-ups: @dd32
7 years ago

  • Keywords reporter-feedback added

Hi @dkurth and welcome to Trac,

Can you please provide some extra details as to the problem you're running into?

Based on your comment in #41006 it sounds like calling wp_redirect()/wp_safe_redirect() isn't performing a redirect at all - would that be correct?

In testing, I can't find any issues - which makes me think this may not be wp_redirect() failing (although it could be) but possibly related to the code you're using to trigger it.

Can you provide a small bit of code to reproduce what you're seeing?
Can you verify it against a clean no-plugins WordPress install?
Is it triggered specifically by a plugin? (If so, what one?)

#2 in reply to: ↑ 1 @dkurth
7 years ago

Replying to dd32:

Hi @dkurth and welcome to Trac,

Can you please provide some extra details as to the problem you're running into?

Based on your comment in #41006 it sounds like calling wp_redirect()/wp_safe_redirect() isn't performing a redirect at all - would that be correct?

In testing, I can't find any issues - which makes me think this may not be wp_redirect() failing (although it could be) but possibly related to the code you're using to trigger it.

Can you provide a small bit of code to reproduce what you're seeing?
Can you verify it against a clean no-plugins WordPress install?
Is it triggered specifically by a plugin? (If so, what one?)

What was happening previously was the wp_redirect($RedirectURL) would return a 1, indicating it was successful, however no forwarding would occur. This was a quick plugin I wrote specifically for a single client, who had different requirements for displaying content. It only point was to test if a user was successfully logged in and had a subscription. If not, it redirects the user to a notice. In this situation, the code had been working great until this last upgrade.

I will admit that it is a bit of a mystery. I had disabled almost all plugins, reverted the Theme backwards and the problem still existed. This left only the wordpress upgrade to 4.8

I did finally find a solution, but it involved not using the wp_redirect call, replacing it with a header call instead. This skips the filters and then the plugin started to work again. You can see the code snippet below, with comments to the right on what was there originally and what I just changed.

	 $user_id = get_current_user_id();
	 if($user_id == 0)
	   {
	    // No one logged in 
	    $RedirectURL = 'https://www.spdressage.com/notloggedin/';
	   //$status = wp_redirect($RedirectURL);	<----- commented out this

           $location = wp_sanitize_redirect($RedirectURL); <----- added this instead
	   header("Location: $location", true, 302);       <----- added this instead
           $status=0;                                      <----- added this instead
           mmd_ca_DebugLine('User Not Logged in', $RedirectURL, $status);
           return;  
	   }

Could there have been something with the filters in the upgrade?

Last edited 7 years ago by SergeyBiryukov (previous) (diff)

#3 in reply to: ↑ 1 @dkurth
7 years ago

  • Severity changed from normal to critical

Replying to dd32:

Hi @dkurth and welcome to Trac,

Can you please provide some extra details as to the problem you're running into?

Based on your comment in #41006 it sounds like calling wp_redirect()/wp_safe_redirect() isn't performing a redirect at all - would that be correct?

In testing, I can't find any issues - which makes me think this may not be wp_redirect() failing (although it could be) but possibly related to the code you're using to trigger it.

I did some more work with this problem, making my plugin a very very simple couple of lines. My site is also the latest wordpress. Any sight you can give me with why these few lines causes the following, would be appreciated:

function mmd_ca_redirect_func() 
{
ob_start(); 
$RedirectURL = 'https://www.mmd-ca.com/';	
$location = wp_sanitize_redirect($RedirectURL);
//header("Location: 'https://www.mmd-ca.com/'", true, 302); <-------CRASHES WITH THE ERROR BELOW:
wp_redirect($location); <--------- NOTHING HAPPENS
}
add_shortcode( 'mmd_redirect', 'mmd_ca_redirect_func' );

ERROR RECEIVED: Warning: Cannot modify header information - headers already sent by

Cam be seem on page: https://mmd-ca.com/test-page/

All this started happening after the last upgrade.

Can you provide a small bit of code to reproduce what you're seeing?
Can you verify it against a clean no-plugins WordPress install?
Is it triggered specifically by a plugin? (If so, what one?)

Last edited 7 years ago by SergeyBiryukov (previous) (diff)

#4 @dkurth
7 years ago

  • Keywords needs-patch added
  • Severity changed from critical to major
  • Summary changed from wp_redirect and wp_safe_redirect calls fails in WP 4.8 to wp_redirect fails with error in WP 4.8

This happens in Wordpress 4.8
In an effort to find out what is causing this problem, I setup a fresh WordPress install with no plugins installed. The same plugin that contains the following code, causes this error.

Warning: Cannot modify header information - headers already sent by (output started at /home/debbiekurth/public_html/wp-content/themes/twentyseventeen/header.php:16) in /home/debbiekurth/public_html/wp-includes/pluggable.php on line 1210

It is a one line plugin call to wp_redirect. Already checked for white spaces and other issues.

<?php
/**
* Plugin Name: A Redirect
* Plugin URI: https://mmd-ca.com/MemberTrack/
* Text Domain: A Redirect
* Description: 
* Version: 1.0.0
* Author: Multimedia Designs, LLC
* Author URI: https://www.mmd-ca.com/
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0
*/
add_shortcode('mmd-redirect', 'mmd_ca_redirect_call');
function mmd_ca_redirect_call()
{
wp_redirect("https://www.mmd-ca.com/");
}
Last edited 7 years ago by SergeyBiryukov (previous) (diff)

#5 @johnbillion
7 years ago

  • Keywords close added

A redirect is just a header that gets sent to the browser. If headers have already been sent (stated in your php warning, presumably due to all of the html output that occurs before shortcodes in the post content are processed) then there's no guarantee the client will respect subsequent headers.

The fact that a redirect in a shortcodes handler ever worked in the past is probably luck more than anything else, and probably didn't work for all visitors to your site.

Are you calling exit after wp_redirect()?

#6 @dkurth
7 years ago

well, here is the thing.

  1. It did work and incredibly well. I had over 100 people testing it from around the world.
  2. When you call exit(). The page stops loading and all you get is a blank page, so for that reason I took that out and it never had it to begin with.
  3. Even with every single plugin, except the redirect, removed, it still does not work.
  1. Ironicly, another site I did with different settings, the header call works, which is inside the wp_redirect()

I tested it again on a fresh version of WordPress, no plugins, except this one version, and used a twentyseventeen theme and it failed with the same error. Looking for if it worked before, then what was changed in the header in this version that needs to be applied now in order for the call to work.

Feel like a bug to me, since the call does not do what the documentation said it would. Your thoughts?

#7 @johnbillion
7 years ago

Can you try the exact same procedure on a WordPress 4.7 installation? There really isn't any way this would have worked previously unless another piece of code was output buffering your entire site, therefore allowing the redirect header to work as expected.

#8 @dkurth
7 years ago

I understand. It is a bit of work to completely restore backwards, but I could do it on my test site. There is something about the header that has changed. Even my theme developer said they had to make some changes in order for their theme to work. I will let you know of the results of going back to previous versions.

#9 @johnbillion
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

If you remove wp_redirect() entirely and use header( "Location: https://www.mmd-ca.com/", true, 302 ); instead, you'll see the same warning from PHP.

I'm going to close this ticket because I'm pretty sure this isn't a bug in WordPress core, but do please re-open it if you find something and think otherwise.

Note: See TracTickets for help on using tickets.