Php 使用set_transient的直接用户
我想每12小时将用户重定向到另一个页面 并希望使用标题(位置:url)和设置 我用这个密码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
$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;
}
}
?>