^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_active_blog_for_user()用法示例

get_active_blog_for_user( int $user_id )

Get one of a user’s active blogs


描述

Returns the user’s primary blog, if they have one and it is active. If it’s inactive, function returns another active blog of the user. If none are found, the user is added as a Subscriber to the Dashboard Blog and that blog is returned.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


参数

$user_id

(int)
(Required)
The unique ID of the user


返回值

(WP_Site|void) The blog object


源代码

File: wp-includes/ms-functions.php

function get_active_blog_for_user( $user_id ) {
	global $wpdb;
	$blogs = get_blogs_of_user( $user_id );
	if ( empty( $blogs ) )
		return;

	if ( !is_multisite() )
		return $blogs[$wpdb->blogid];

	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
	$first_blog = current($blogs);
	if ( false !== $primary_blog ) {
		if ( ! isset( $blogs[ $primary_blog ] ) ) {
			update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
			$primary = get_site( $first_blog->userblog_id );
		} else {
			$primary = get_site( $primary_blog );
		}
	} else {
		//TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
		add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
		update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
		$primary = $first_blog;
	}

	if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
		$blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
		$ret = false;
		if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
			foreach ( (array) $blogs as $blog_id => $blog ) {
				if ( $blog->site_id != $wpdb->siteid )
					continue;
				$details = get_site( $blog_id );
				if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
					$ret = $blog;
					if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
update_user_meta( $user_id, 'primary_blog', $blog_id );
					if ( !get_user_meta($user_id , '源代码 _domain', true) )
update_user_meta( $user_id, '源代码 _domain', $blog->domain );
					break;
				}
			}
		} else {
			return;
		}
		return $ret;
	} else {
		return $primary;
	}
}

更新日志

Version 描述
MU 1.0 Introduced.

相关函数

Uses

  • wp-includes/ms-blogs.php:
    get_site()
  • wp-includes/load.php:
    is_multisite()
  • wp-includes/user.php:
    get_blogs_of_user()
  • wp-includes/user.php:
    get_user_meta()
  • wp-includes/user.php:
    update_user_meta()
  • wp-includes/ms-functions.php:
    add_user_to_blog()
  • Show 1 more use


Used By

  • wp-includes/link-template.php:
    get_dashboard_url()
  • wp-includes/class-wp-admin-bar.php:
    WP_Admin_Bar::initialize()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::wp_getUsersBlogs()

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈