wordpress “wp_redirect”不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19587154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:24:13  来源:igfitidea点击:

'wp_redirect' is not working

wordpressredirect

提问by Rincver

There is an HTML form input. Here's the code:

有一个 HTML 表单输入。这是代码:

<?php if(isset($_POST['login'])) { 
    wp_redirect("/"); 
}

<form accept-charset="UTF-8" method="post" >
    ...
    <center><input name="login" type="submit" value="вход" />
</form>

But redirect doesn't work. Install debug plugin redirects to wp, that's what it showed.

但是重定向不起作用。安装调试插件重定向到 wp,这就是它显示的内容。

http://i.stack.imgur.com/Im4eE.png

http://i.stack.imgur.com/Im4eE.png

PS:

PS:

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

It does not work either.

它也不起作用。

回答by Pratyush Deb

Use the follwing code:-

使用以下代码:-

function app_output_buffer() {
    ob_start();
} // soi_output_buffer
add_action('init', 'app_output_buffer');

Or add ob_start() as first line of your own function which hook into 'init'

或者将 ob_start() 添加为您自己的函数的第一行,该函数挂接到 'init'

Don't forget to add

不要忘记添加

exit();

immediately after your call to

在您致电后立即

wp_redirect($url);

link:

关联:

https://tommcfarlin.com/wp_redirect-headers-already-sent/

https://tommcfarlin.com/wp_redirect-headers-already-sent/

回答by revo

I think your code doesn't begin with that ifcondition!

我认为您的代码不是以该if条件开头的!

wp_redirectwill send an header, so printing/echoing something before it, will have result in failure.

wp_redirect将发送一个标题,因此在它之前打印/回显某些内容将导致失败。

So check and see if before this:

因此,在此之前检查并查看:

if(isset($_POST['login'])) 
{
    wp_redirect("/");
    exit;
}

there is no character out put. Also do not forget to put exit;right after wp_redirect.

没有字符输出。也不要忘记放在exit;正确的之后wp_redirect

回答by Bhautik Nada

Just use this as per below:

只需按照以下方式使用它:

ob_clean();
$url = get_home_url() . '/login';
wp_redirect($url);
exit();

Or you could use Javascript as well for redirection purpose.

或者您也可以将 Javascript 用于重定向目的。

<script>window.location='http://www.google.com'</script>

回答by abdallah Nofal

I faced same problem and none of these solutions worked for me.

我遇到了同样的问题,但这些解决方案都不适合我。

The only thing that I noticed is different at the page is that I used wp_redirectbelow get_header()and it will work fine if you used it above it.

我注意到页面上唯一不同的是我wp_redirect在下面get_header()使用了它,如果你在它上面使用它会正常工作。

回答by Fury

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.

确保您没有:get_header();或任何可能在模板中创建页眉和页脚等内容的 wordpress 函数。否则重定向将不起作用。

Some developers try to clear the page by using ob_start();but if you have content in your page even if you use ob_start();the redirection won't work.

一些开发人员尝试通过使用来清除页面,ob_start();但是如果您的页面中有内容,即使您使用ob_start();重定向也不起作用。

and then simply try this code:

然后只需尝试以下代码:

wp_redirect(get_permalink($post->ID));
exit;

回答by BrownEyes

Try the following, which also forces on error reporting:

尝试以下操作,这也会强制执行错误报告:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);


flush();
header("Location: http://www.website.com/");
die('should have redirected by now');

From PHP header redirect not working

PHP 标头重定向不起作用

Edit:

编辑:

Since it's giving you the headers already sentwarning, try adding the following at the very beginningof your code:

由于它向您headers already sent发出警告,请尝试在代码的开头添加以下内容:

ob_start();

ob_start();

The long term answer is that all output from your PHP scripts should be buffered in variables. This includes headers and body output. Then at the end of your scripts do any output you need.

The very quick fix for your problem will be to add ob_start(); as the very first thing in your script if you only need it in this one script. If you need it in all your scripts add it as the very first thing in your header.php file.

This turns on PHP's output buffering feature. In PHP when you output something (do an echo or print) if has to send the HTTP headers at that time. If you turn on output buffering you can output in the script but PHP doesn't have to send the headers until the buffer is flushed. If you turn it on and don't turn it off PHP will automatically flush everything in the buffer after the script finishes running. There really is no harm in just turning it on in almost all cases and could give you a small performance increase under some configurations...

长期的答案是 PHP 脚本的所有输出都应该缓冲在变量中。这包括标题和正文输出。然后在脚本的末尾执行您需要的任何输出。

解决您的问题的非常快速的方法是添加 ob_start(); 如果您只在这个脚本中需要它,那么作为脚本中的第一件事。如果您在所有脚本中都需要它,请将其添加为 header.php 文件中的第一件事。

这将打开 PHP 的输出缓冲功能。在 PHP 中,当您输出某些内容(执行回显或打印)时,如果必须在那时发送 HTTP 标头。如果您打开输出缓冲,您可以在脚本中输出,但 PHP 不必在缓冲区刷新之前发送标头。如果您打开它而不关闭它,PHP 将在脚本完成运行后自动刷新缓冲区中的所有内容。在几乎所有情况下打开它确实没有害处,并且可以在某些配置下为您带来小的性能提升......

From Warning: Cannot modify header information - headers already sent..

来自警告:无法修改标头信息 - 标头已发送..