对于已经开放注册的WordPress站点,用户登录后的页面跳转是必须要考虑的。今天就说说wordpress登录后跳转或指定页面,以下代码都可以添加到主题的 functions.php
wordpress登录后跳转指定页面
- /**
- * 用户注册成功后自动登录,并跳转到指定页面
- */
- function auto_login_new_user( $user_id ) {
- // 用户注册后自动登录
- wp_set_current_user($user_id);
- wp_set_auth_cookie($user_id);
- // 这里跳转到 http://域名/about 页面,请根据自己的需要修改
- wp_redirect( home_url().’about’ );
- exit;
- }
- add_action( ‘user_register’, ‘auto_login_new_user’);
一定时间内登录重定向
- /**
- * 注册一定时间内登录重定向到指定页面
- */
- function time_limit_login_redirect( $to, $requested, $user ){
- if( !isset( $user->user_login ) ){
- return $to;
- }
- $regtime = strtotime($user->user_registered);
- $now = strtotime(“now”);
- $diff = $now – $regtime;
- $hours = $diff / 60 / 60;
- if( $hours < 48 ){ // 注册后48小时内登录重定向到该页面
- return “/about”;
- } else {
- return admin_url(); //WP管理后台
- }
- }
- add_filter(‘login_redirect’, ‘time_limit_login_redirect’, 10, 3);
注册后的一定时间内,不管是第几次登录,都跳转到指定页面。