Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在不使用javascript的情况下,在wordpress中n秒后重定向到另一个页面?_Php_Wordpress_Redirect - Fatal编程技术网

Php 如何在不使用javascript的情况下,在wordpress中n秒后重定向到另一个页面?

Php 如何在不使用javascript的情况下,在wordpress中n秒后重定向到另一个页面?,php,wordpress,redirect,Php,Wordpress,Redirect,以下问题: 我在php中检查插件页面的条件,如果条件为false,我想重定向到另一个页面 使用PHP,它将是: header('Refresh: 10; URL=http://yoursite.com/page.php'); 如果你想在10秒后重定向。但是在Wordpress中会出现错误“headers ready sent” 它的wordpress功能是: exit( wp_redirect( admin_url( 'admin.php?page=your_page' ) ) ); 但此函

以下问题: 我在php中检查插件页面的条件,如果条件为false,我想重定向到另一个页面

使用PHP,它将是:

header('Refresh: 10; URL=http://yoursite.com/page.php');
如果你想在10秒后重定向。但是在Wordpress中会出现错误“headers ready sent”

它的wordpress功能是:

exit( wp_redirect( admin_url( 'admin.php?page=your_page' ) ) );
但此函数没有时间参数


我知道如何使用javascript重定向,但我更愿意使用php/wordpress函数。有办法做到这一点吗?

没有,一旦PHP发送了标题(在本例中是整个页面),n秒后就没有办法做到这一点

一个可能的错误选项是让PHP使用Sleep(5)保持渲染一段时间,但这不是一个好选项

最佳解决方案: 重写函数“wp_redirect”,并设置一个类似这样的附加参数

wp_redirect($page, $interval = 20)

请确保设置$inverval=20,否则使用此函数的其他脚本可能会失败。

php代码是服务器端的,经过处理后传递给客户端

简易解决方法:


使用特殊标题并输出普通html重定向标题

您必须能够以某种方式修改文档的
部分,并添加以下元标记:

<meta http-equiv="refresh" content="10; URL=http://yoursite.com/page.php">

这将根据模板的结构有所不同

在wordpress中修改head部分(而不是使用模板)的一般解决方案是添加一个操作:



好的,似乎没有简单的PHP解决方案来解决这个问题,所以我创建了一个javascript解决方案。似乎是处理重定向的最简单方法:

if($my_condition == false){ ?>
    <script type="text/javascript">
       $(document).ready(function() {
           var redirection_link=<?php echo json_encode(admin_url( 'admin.php?page=my_plugins_settings_page' )); ?>;                
           alert('Please check your Settings first!');
           window.location.replace(redirection_link);
        });
    </script>
<?php
}
if($my_condition==false){?>
$(文档).ready(函数(){
var_link=;
警报('请先检查您的设置!');
window.location.replace(重定向链接);
});

也许是Meta刷新?AOK。thx。但是因为它是一个设置页面,而不是一个普通的页面或帖子站点,所以选择或更改模板不是那么容易的。当我的代码启动时,GETX头()函数已经被启动……然后考虑添加一个动作,它将在构建头时运行。
if($my_condition == false){ ?>
    <script type="text/javascript">
       $(document).ready(function() {
           var redirection_link=<?php echo json_encode(admin_url( 'admin.php?page=my_plugins_settings_page' )); ?>;                
           alert('Please check your Settings first!');
           window.location.replace(redirection_link);
        });
    </script>
<?php
}