_access_denied_splash()
云策文档标注
概述
_access_denied_splash() 函数用于在用户尝试访问无权访问的站点仪表板时,显示访问被拒绝消息。它检查用户登录状态和站点权限,并根据情况输出错误信息或提供快捷链接。
关键要点
- 函数仅在用户已登录且非网络管理员界面时触发
- 通过 get_blogs_of_user() 获取用户所属站点列表,检查当前站点权限
- 若无权限,使用 wp_die() 显示自定义错误消息,状态码为 403
- 错误消息包含站点名称和联系网络管理员的提示
- 如果用户有其他站点,会提供“您的站点”列表和快捷链接(如访问仪表板、查看站点)
- 依赖多个 WordPress 核心函数,如 is_user_logged_in()、wp_list_filter()、get_bloginfo() 等
代码示例
function _access_denied_splash() {
if ( ! is_user_logged_in() || is_network_admin() ) {
return;
}
$blogs = get_blogs_of_user( get_current_user_id() );
if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) {
return;
}
$blog_name = get_bloginfo( 'name' );
if ( empty( $blogs ) ) {
wp_die(
sprintf(
__( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ),
$blog_name
),
403
);
}
$output = '' . sprintf(
__( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ),
$blog_name
) . '';
$output .= '' . __( 'If you reached this screen by accident and meant to visit one of your own sites, here are some shortcuts to help you find your way.' ) . '';
$output .= '' . __( 'Your Sites' ) . '';
$output .= '';
foreach ( $blogs as $blog ) {
$output .= '';
$output .= "{$blog->blogname}";
$output .= 'userblog_id ) ) . '">' . __( 'Visit Dashboard' ) . ' | ' .
'userblog_id ) ) . '">' . __( 'View Site' ) . '';
$output .= '';
}
$output .= '';
wp_die( $output, 403 );
}注意事项
- 函数自 WordPress 3.2.0 版本引入
- 错误消息使用 __() 进行国际化处理,便于翻译
- 链接使用 esc_url() 进行清理,确保安全性
- 适用于多站点环境,处理用户跨站点访问权限问题
原文内容
Displays an access denied message when a user tries to view a site’s dashboard they do not have access to.
Source
function _access_denied_splash() {
if ( ! is_user_logged_in() || is_network_admin() ) {
return;
}
$blogs = get_blogs_of_user( get_current_user_id() );
if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) {
return;
}
$blog_name = get_bloginfo( 'name' );
if ( empty( $blogs ) ) {
wp_die(
sprintf(
/* translators: 1: Site title. */
__( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ),
$blog_name
),
403
);
}
$output = '<p>' . sprintf(
/* translators: 1: Site title. */
__( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ),
$blog_name
) . '</p>';
$output .= '<p>' . __( 'If you reached this screen by accident and meant to visit one of your own sites, here are some shortcuts to help you find your way.' ) . '</p>';
$output .= '<h3>' . __( 'Your Sites' ) . '</h3>';
$output .= '<table>';
foreach ( $blogs as $blog ) {
$output .= '<tr>';
$output .= "<td>{$blog->blogname}</td>";
$output .= '<td><a href="' . esc_url( get_admin_url( $blog->userblog_id ) ) . '">' . __( 'Visit Dashboard' ) . '</a> | ' .
'<a href="' . esc_url( get_home_url( $blog->userblog_id ) ) . '">' . __( 'View Site' ) . '</a></td>';
$output .= '</tr>';
}
$output .= '</table>';
wp_die( $output, 403 );
}
Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |