Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.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 使用set_transient的直接用户_Php_Redirect_Wordpress_Transient - Fatal编程技术网

Php 使用set_transient的直接用户

Php 使用set_transient的直接用户,php,redirect,wordpress,transient,Php,Redirect,Wordpress,Transient,我想每12小时将用户重定向到另一个页面 并希望使用标题(位置:url)和设置 我用这个密码 $transient = get_transient( 'name_my_transient' ); if ( empty( $transient ) ){ function redirect() { $data = 'redirected'; return $data; header('location: http://www.google.com

我想每12小时将用户重定向到另一个页面 并希望使用标题(位置:url)和设置 我用这个密码

$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,它总是重定向用户

您的反应很好,但我认为您在代码组织上犯了一些错误,请尝试以下方法:

<?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;
    }
}
?>