admin管理员组

文章数量:1134247

I'm writing a WatuPro plugin customization. I need to redirect users to specific url when an exam is completed. I have to do it by code because i need to attach some dynamic get parameters to the request. To intercept an exam completion i have the action :

watupro_completed_exam

i this action i'm trying to achive the redirection:

 $complete_url="http://localhost/mysite/resultpage?email=".$email."&param_1=".$param_1;

 wp_redirect( $complete_url );

the redirect works, because i see "resultpage content" but the url in the browser not change, it remains the same.

So my question is not related to Watu Pro plugin, it is a general question: In which case wp_redirect can change content of the page without change the url ?

This is my .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
</IfModule>

# END WordPress

I'm writing a WatuPro plugin customization. I need to redirect users to specific url when an exam is completed. I have to do it by code because i need to attach some dynamic get parameters to the request. To intercept an exam completion i have the action :

watupro_completed_exam

i this action i'm trying to achive the redirection:

 $complete_url="http://localhost/mysite/resultpage?email=".$email."&param_1=".$param_1;

 wp_redirect( $complete_url );

the redirect works, because i see "resultpage content" but the url in the browser not change, it remains the same.

So my question is not related to Watu Pro plugin, it is a general question: In which case wp_redirect can change content of the page without change the url ?

This is my .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
</IfModule>

# END WordPress
Share Improve this question asked Sep 7, 2023 at 12:40 user31929user31929 1012 bronze badges 1
  • redirections can only happen before any HTML output occurs, and should be followed by an exit; statement. Unfortunately there's no information about where and when your code is running, or where the variables used to construct the URL come from. Additionally you can't ask questions about 3rd party plugins here as they're offtopic and not in this stacks scope, you should ask in a WatuPro user community/group or their official support routes for help working with that plugin – Tom J Nowell Commented Sep 7, 2023 at 13:37
Add a comment  | 

1 Answer 1

Reset to default 0

In which case wp_redirect can change content of the page without change the url ?

No, if used correctly there are no situations where wp_redirect interferes with the content of a page. It's job is to do a HTTP redirect telling the browser to stop and start a new fresh request using a new URL, and that is its only job. Your HTAccess is not a factor in this. If the URL did not change then there was no redirect.

I suspect that you've either called the function in the middle of a page when it is too late to redirect, and that you also did not exit; after calling it like the documented examples show, and it's very likely that your PHP error log is full of warnings and errors about "headers have already been sent".

How To Redirect

To use wp_redirect, it must be called before any output is sent. The moment something is sent to the browser, be that whitespace or the opening HTML tag, all opportunities to use wp_redirect have ended. This means you can never use it in a shortcode widget or block.

If you want to use it, it must be done on a hook, and one that runs before the template is loaded. It must also have an exit statement immediately after it.

Note that when I say HTML, I mean any HTML, not just post HTML, but headers, meta tags, <head> or even blank spaces. From the moment of the first echo or ?> PHP will send out HTTP headers telling the browser to expect HTML and your chance to redirect has gone and cannot be recovered. The only way to redirect at that point is to do it in javascript or HTML.

Which Hook?

Which hook you should use depends on how the plugin you're working with is built. There are a number of generic hooks that might work such as init/after_theme_setup but this cannot be done from a theme template or once HTML has been sent to the browser.

Your question states that you're trying to use watupro_completed_exam, you would need to ask their support or read the plugins code to identify when this hook fires to see if it can be used for this.

This means what you want may not be fixable without the help of WatuPro because only they can change when the watupro_completed_exam hook fires, which may not be possible to do, and there is no generic WordPress fix for this specific use case.

本文标签: actionsWp redirection doesn39t not change url in browser