如何在不使用javascript的情况下在wordpress中n秒后重定向到另一个页面? [英] How to redirect to another page after n seconds in wordpress without using javascript?

查看:43
本文介绍了如何在不使用javascript的情况下在wordpress中n秒后重定向到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following problem: I check a condition for a plugins' page in php and if it's false I want to redirect to another page.

如果使用 PHP,那就是:

With PHP it would simply be:

header('Refresh: 10; URL=http://yoursite.com/page.php');

如果您想在 10 秒后重定向.但是在 Wordpress 中会出现错误headers already sent".

if you want to redirect after 10 seconds. But in Wordpress the error "headers already sent" will occur.

它的 wordpress 函数是:

The wordpress function for it would be:

exit( wp_redirect( admin_url( 'admin.php?page=your_page' ) ) );

但是这个函数没有时间参数.

but there is no time parameter for this function.

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

I know how to redirect with javascript but I'd rather do it with php/wordpress functions. Is there a way to do this?

推荐答案

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

You will have to be able to modify the <head> section of the document somehow and add the following meta tag:

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

这会有所不同,具体取决于您的模板结构.

This will be different depending on how you have your templates structured.

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

The general solution to modifying the head section in wordpress (not by using templates) is to add a wp_head action:

<?php

add_action('wp_head', 'some_function');
function some_function() {
    if($someCondition) { ?>
        <meta http-equiv="refresh" content="10; URL=http://yoursite.com/page.php"> <?php
    }
}

这篇关于如何在不使用javascript的情况下在wordpress中n秒后重定向到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆