使用set_transient指导用户



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

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

最新更新