WordPress函数文档auth_redirect()
判断用户是否登录,没有登录则跳转到登录页面 描述 译文 auth_redirect() 是一个简单函数,要求用…
判断用户是否登录,没有登录则跳转到登录页面
描述
译文
auth_redirect()
是一个简单函数,要求用户访问页面前登录。
缺省用法
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php auth_redirect() ?>
|
页面调用此代码时,代码会查看访问当前页面的用户是否登录。如果用户未登录则将用户重定向至登录页面。登录后可将用户直接转到登录前所访问的页面。
参数
该函数不接受参数。
相关资源
is_user_logged_in(),wp_redirect()
原文
Checks user is logged in, if not it redirects them to login page.
When this code is called from a page, it checks to see if the user viewing the page is logged in. If the user is not logged in, they are redirected to the login page. The user is redirected in such a way that, upon logging in, they will be sent directly to the page they were originally trying to access.
用法
<?php auth_redirect(); ?>
参数
This function accepts no parameters.
返回值
Function redirects or exits, does not return
注意
As a pluggable function, you can redefine it and your version will be used instead.
历史
添加于 版本: 1.5
源文件
auth_redirect() 函数的代码位于 wp-includes/pluggable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Checks if a user is logged in, if not it redirects them to the login page.
*
* @since 1.5.0
*/
function auth_redirect() {
// Checks if a user is logged in, if not redirects them to the login page
$secure = ( is_ssl() || force_ssl_admin() );
/**
* Filter whether to use a secure authentication redirect.
*
* @since 3.1.0
*
* @param bool $secure Whether to use a secure authentication redirect. Default false.
*/
$secure = apply_filters( ‘secure_auth_redirect’, $secure );
// If https is required and request is http, redirect
if ( $secure && !is_ssl() && false !== strpos($_SERVER[‘REQUEST_URI’], ‘wp-admin’) ) {
if ( 0 === strpos( $_SERVER[‘REQUEST_URI’], ‘http’ ) ) {
wp_redirect( set_url_scheme( $_SERVER[‘REQUEST_URI’], ‘https’ ) );
exit();
} else {
wp_redirect( ‘https://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’] );
exit();
}
}
if ( is_user_admin() ) {
$scheme = ‘logged_in’;
} else {
/**
* Filter the authentication redirect scheme.
*
* @since 2.9.0
*
* @param string $scheme Authentication redirect scheme. Default empty.
*/
$scheme = apply_filters( ‘auth_redirect_scheme’, ” );
}
if ( $user_id = wp_validate_auth_cookie( ”, $scheme) ) {
/**
* Fires before the authentication redirect.
*
* @since 2.8.0
*
* @param int $user_id User ID.
*/
do_action( ‘auth_redirect’, $user_id );
// If the user wants ssl but the session is not ssl, redirect.
if ( !$secure && get_user_option(‘use_ssl’, $user_id) && false !== strpos($_SERVER[‘REQUEST_URI’], ‘wp-admin’) ) {
if ( 0 === strpos( $_SERVER[‘REQUEST_URI’], ‘http’ ) ) {
wp_redirect( set_url_scheme( $_SERVER[‘REQUEST_URI’], ‘https’ ) );
exit();
} else {
wp_redirect( ‘https://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’] );
exit();
}
}
return; // The cookie is good so we’re done
}
// The cookie is no good so force login
nocache_headers();
$redirect = ( strpos( $_SERVER[‘REQUEST_URI’], ‘/options.php’ ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( ‘http://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’] );
$login_url = wp_login_url($redirect, true);
wp_redirect($login_url);
exit();
}
endif;
if ( !function_exists(‘check_admin_referer’) ) :
|
相关
is_user_logged_in(), wp_redirect()
- 原文:http://codex.wordpress.org/Function_Reference/auth_redirect
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!