wp_redirect()函数不起作用
7 个回答
- 投票数
-
- 2012-03-21
这里有两件事是错误的:
- 请勿使用
$post->guid
作为网址 - 使用
wp_redirect()
(请参阅法典)wp_redirect()
不会自动退出,并且几乎总是在退出之后.
//.....有问题的代码 $post_id=wp_insert_post($new_post); $ url=get_permalink($post_id); wp_redirect($ url); 出口();
Two things wrong here:
- Don't use
$post->guid
as an url - You must
exit()
after usingwp_redirect()
(see the Codex)wp_redirect()
does not exit automatically and should almost always be followed by exit.
//..... code as in question $post_id = wp_insert_post($new_post); $url = get_permalink( $post_id ); wp_redirect($url); exit();
-
击败你30秒:DBeat you by 30 seconds :D
- 0
- 2012-03-21
- soulseekah
-
如果我运行页面控制台显示302 Found,这将无法正常工作 479毫秒 jquery ... r=1.7.1(第4行) GET http://localhost/wordpress/newpages-17/ 200 OK 1.2s正在加载..........this is not working, if i run the page console show 302 Found 479ms jquery...r=1.7.1 (line 4) GET http://localhost/wordpress/newpages-17/ 200 OK 1.2s loading..........
- 0
- 2012-03-21
- SANS780730
-
多数民众赞成在JS错误.与`wp_redirect`无关.上面的答案是正确的方法,因此您必须做其他错误的事情.Thats a JS error. Nothing to do with `wp_redirect`. The above answer is the correct way to do it, so you must be doing something else wrong.
- 2
- 2012-03-21
- Stephen Harris
-
对不起.它只显示GET localhost/wordpress/newpages-17 200 OK 1.2s loading .......sorry.it show only GET localhost/wordpress/newpages-17 200 OK 1.2s loading..........
- 0
- 2012-03-21
- SANS780730
-
@StephenHarris您是否可以在http://wordpress.stackexchange.com/q/76991/10413上查看我的重定向问题,我也尝试使用$pid从此答案中尝试您的代码,但仍无法使它正常工作.谢谢@StephenHarris would you mind looking over my redirect question at http://wordpress.stackexchange.com/q/76991/10413 I've also tried your code from this answer using $pid but still can't get it to work. Thanks
- 0
- 2012-12-22
- Anagio
-
这个问题有很多见解,因此,如果解决了您的问题,请考虑接受此答案.这样,问题就会按照回答显示出来,并帮助我们保持网站整洁.谢谢.This question gets a huge number of views, so please consider accepting this answer if it solved your problem. That way the question shows up as answered and helps us keep the site tidy. Thanks.
- 0
- 2016-07-23
- Andy Macaulay-Brook
-
- 2015-12-10
我有一个简单的解决方案,请阅读:
-
如果在主题文件中使用
wp_redirect($ url)
,但无法正常工作,请在函数文件中添加ob_clean()ob_start()
顶部. -
如果在插件中使用,请在顶部的主插件文件中添加
ob_clean()ob_start()
.
并确保在wp_redirect($ url)之后添加了
exit()函数,如下所示:
$ url='http://example.com'; wp_redirect($ url); 出口();
I have a simple solution, please read:
If you are using
wp_redirect($url)
in theme files, and it is not working addob_clean() ob_start()
in your function file on top.If using in plugin add
ob_clean() ob_start()
in the main plugin file on top.
And make sure you have added
exit() function after wp_redirect($url)
Like this:$url = 'http://example.com'; wp_redirect($url); exit();
-
这解决了Mozilla Firefox返回200而不是302进行重定向的问题.Chrome重定向,而Firefox不重定向.此修复程序有帮助.谢谢!This solves the issue with Mozilla Firefox returning 200 instead of 302 for the redirection to take place. Chrome redirects while Firefox doesn't. This fix helps. Thank you!
- 0
- 2018-09-12
- El'Magnifico
-
如果要创建插件或设计模板,这是更详细的答案.为我工作.This is a more detailed answer if you are creating a plugin or designing template. worked for me.
- 0
- 2019-07-10
- Sayed Mohd Ali
-
我一直在努力以自己的自定义主题来完成这项工作...就像魅力一样...I've been struggling to make this work in my custom theme... Works like a charm...
- 0
- 2019-10-08
- ShivangiBilora
-
- 2013-05-01
我不确定这是否有帮助...但是我发现我的模板中有一些代码 而我以这种方式从get_header()开始:
<?php /** * .. Template comments */ get_header(); if(...) { ... if(...) { ... wp_redirect($url); exit(); } } ?>
并且收到了先前发送的同一标题问题...我所做的只是将get_header()移至块的末尾,瞧!!
<?php /** * .. Template comments */ if(...) { ... if(...) { ... wp_redirect($url); exit(); } } get_header(); ?>
没有插件被禁用.一切都很好...如果这适合您,您可以尝试一下
I am not sure if this will help... but I found that I had some code in a template and I was starting with get_header() in this way:
<?php /** * .. Template comments */ get_header(); if(...) { ... if(...) { ... wp_redirect($url); exit(); } } ?>
and was getting the same issue of header previously sent... What I did was just move get_header() to the end of the block and voila!!!
<?php /** * .. Template comments */ if(...) { ... if(...) { ... wp_redirect($url); exit(); } } get_header(); ?>
No plugin was disabled. and everything was ok... you may give a try if this works for you
-
如果您有权访问主题源,这是一种很好的方法.重定向应该在调用get_header之前运行.This is a good way to do it, if you have access to the theme source. Redirects should run before the call to `get_header`.
- 0
- 2013-05-01
- s_ha_dum
-
删除get_header()也对我有用!removing get_header() also worked for me!
- 0
- 2014-10-31
- Magico
-
我敢打赌,这是导致大多数人为wp_redirect苦苦挣扎的最常见原因I'd bet this is the most common cause for most people struggling with `wp_redirect` not working
- 0
- 2019-02-25
- joehanna
-
- 2012-03-21
从不使用帖子的GUID值,它不必与帖子的真实URL匹配.
http://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note
wp_redirect( get_permalink( $post_id ) ); exit(); // always exit
还要确保
wp_redirect
没有被其他东西塞住,以防止其正确执行其工作.停用所有插件,然后还原为二十/十一.Never ever use the post GUID value, it does not have to match the real URL of the post.
http://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note
wp_redirect( get_permalink( $post_id ) ); exit(); // always exit
Also, make sure
wp_redirect
is not plugged by something else which prevents it from doing its job correctly. Deactivate all plugins and revert to Twenty Ten/Eleven to check.-
+1可插拔的wp_redirect的好调用+1 good call on `wp_redirect` being pluggable
- 0
- 2012-03-21
- Stephen Harris
-
感谢您....thaning you....
- 0
- 2012-03-21
- SANS780730
-
- 2019-01-16
确保您没有:
get_header();
或任何可能在模板中创建诸如页眉和页脚之类内容的wordpress函数.否则重定向将不起作用.一些开发人员尝试通过使用
ob_start();
来清除页面,但是即使您使用了ob_start();
,即使页面中包含内容,重定向也不会成功.工作.然后只需尝试以下代码:
wp_redirect(get_permalink($post->ID)); exit;
Make sure you don't have:
get_header();
or any wordpress function that potentially creates contents like header and footer in your template. Otherwise the redirection won't work.Some developers try to clear the page by using
ob_start();
but if you have content in your page even if you useob_start();
the redirection won't work.and then simply try this code:
wp_redirect(get_permalink($post->ID)); exit;
-
- 2019-08-06
if( is_page( ['wfp-dashboard', 'wfp-checkout'] ) ){ if(!is_user_logged_in()){ @ob_flush(); @ob_end_flush(); @ob_end_clean(); wp_redirect( wp_login_url() ); exit(); } }
if( is_page( ['wfp-dashboard', 'wfp-checkout'] ) ){ if(!is_user_logged_in()){ @ob_flush(); @ob_end_flush(); @ob_end_clean(); wp_redirect( wp_login_url() ); exit(); } }
-
您能否提供一些有关如何解决此问题的解释?我不确定为什么在代码中有输出缓冲区,它们有`@`运算符,`@`不能防止错误发生,它只是将它们从错误日志中隐藏了Can you include some explanation of how this fixes the issue? I'm not sure why there are output buffers in the code, and they have the `@` operator, `@` doesn't prevent errors from happening, it just hides them from the error log
- 0
- 2020-07-22
- Tom J Nowell
-
- 2020-07-22
已经发送的标题是主要原因.由于标头已经发送,因此它无法重新发送并且无法重定向.像init钩子一样在标题之前使用.
add_action('init', 'your_app_function');
header already sent is main reason. As header already sent, so its unable to resend it and fails to redirect. Use before header like in init hook.
add_action('init', 'your_app_function');
wp_redirect($post->guid)
不起作用.我该如何解决?这是我的代码: