首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >直接使用set_transient的用户

直接使用set_transient的用户
EN

Stack Overflow用户
提问于 2015-04-08 15:40:33
回答 1查看 209关注 0票数 2

我想重定向用户每12小时到另一个页面,并希望使用标题(位置:网址)和set_transient我使用这些代码

代码语言:javascript
复制
$transient = get_transient( 'name_my_transient' );
if ( empty( $transient ) ){
    function redirect() {
        $data = 'redirected';
        return $data;
        header('location: http://www.google.com');
    }
add_action('wp_head', 'redirect');      
set_transient('name_my_transient', $data, 60*60*12 ); 
}

如果我删除return $data,它总是重定向用户

EN

回答 1

Stack Overflow用户

发布于 2015-04-10 17:42:16

你的反应很好,但我认为你在代码组织上犯了一些错误,试试这个:

代码语言:javascript
复制
<?php
// Call your function with a top priority
add_action( 'wp_head', 'redirect', 1 );

function redirect() {
    // Get the transien
    $transient = get_transient( 'name_my_transient' );

    // If the data do not exist, set it and redirect user
    if ( $transient === false ) {

        // Set the transient for 12 hours
        set_transient('name_my_transient', 'redirected', 12 * HOUR_IN_SECONDS ); 

        // Redirect the user with wp_redirect
        wp_redirect( 'http://www.google.com' ); exit;
    }
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29508726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档