ms_site_check()
云策文档标注
概述
ms_site_check() 函数用于检查当前博客的状态,包括是否被删除、未激活、归档或标记为垃圾。如果博客未通过检查,默认会终止执行并显示消息,但可通过自定义 drop-in 文件来修改此行为。
关键要点
- 检查博客状态:包括 deleted、archived、spam 等属性。
- 返回结果:成功时返回 true,否则返回 drop-in 文件路径或终止执行。
- 自定义消息:通过 wp-content/blog-deleted.php、blog-inactive.php 和 blog-suspended.php 文件可覆盖默认错误消息。
- 超级管理员权限:超级管理员可绕过检查,直接返回 true。
- 使用 Hook:可通过 apply_filters('ms_site_check', null) 过滤检查逻辑。
代码示例
function ms_site_check() {
$check = apply_filters( 'ms_site_check', null );
if ( null !== $check ) {
return true;
}
if ( is_super_admin() ) {
return true;
}
$blog = get_site();
if ( '1' === $blog->deleted ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) ) {
return WP_CONTENT_DIR . '/blog-deleted.php';
} else {
wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
}
}
if ( '2' === $blog->deleted ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
return WP_CONTENT_DIR . '/blog-inactive.php';
} else {
$admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_network()->domain ) );
wp_die(
sprintf(
__( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
sprintf( '%1$s', $admin_email )
)
);
}
}
if ( '1' === $blog->archived || '1' === $blog->spam ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) ) {
return WP_CONTENT_DIR . '/blog-suspended.php';
} else {
wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
}
}
return true;
}注意事项
- 函数在 WordPress 3.0.0 版本引入,主要用于多站点环境。
- 依赖函数包括 get_site()、is_super_admin()、wp_die() 等,确保相关文件已加载。
- 自定义 drop-in 文件需放置在 wp-content 目录下,否则会使用默认消息。
原文内容
Checks status of current blog.
Description
Checks if the blog is deleted, inactive, archived, or spammed.
Dies with a default message if the blog does not pass the check.
To change the default message when a blog does not pass the check, use the wp-content/blog-deleted.php, blog-inactive.php and blog-suspended.php drop-ins.
Source
function ms_site_check() {
/**
* Filters checking the status of the current blog.
*
* @since 3.0.0
*
* @param bool|null $check Whether to skip the blog status check. Default null.
*/
$check = apply_filters( 'ms_site_check', null );
if ( null !== $check ) {
return true;
}
// Allow super admins to see blocked sites.
if ( is_super_admin() ) {
return true;
}
$blog = get_site();
if ( '1' === $blog->deleted ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) ) {
return WP_CONTENT_DIR . '/blog-deleted.php';
} else {
wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
}
}
if ( '2' === $blog->deleted ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
return WP_CONTENT_DIR . '/blog-inactive.php';
} else {
$admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_network()->domain ) );
wp_die(
sprintf(
/* translators: %s: Admin email link. */
__( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
sprintf( '<a href="mailto:%1$s">%1$s</a>', $admin_email )
)
);
}
}
if ( '1' === $blog->archived || '1' === $blog->spam ) {
if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) ) {
return WP_CONTENT_DIR . '/blog-suspended.php';
} else {
wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
}
}
return true;
}
Hooks
- apply_filters( ‘ms_site_check’, bool|null $check )
-
Filters checking the status of the current blog.
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |