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

wordpress函数add_user_to_blog()用法示例

add_user_to_blog( int $blog_id,  int $user_id,  string $role )

Adds a user to a blog.


描述

Use the ‘add_user_to_blog’ action to fire an event when users are added to a blog.


参数

$blog_id

(int)
(Required)
ID of the blog you’re adding the user to.

$user_id

(int)
(Required)
ID of the user you’re adding.

$role

(string)
(Required)
The role you want the user to have


返回值

(true|WP_Error)


源代码

File: wp-includes/ms-functions.php

function add_user_to_blog( $blog_id, $user_id, $role ) {
	switch_to_blog($blog_id);

	$user = get_userdata( $user_id );

	if ( ! $user ) {
		restore_current_blog();
		return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
	}

	if ( !get_user_meta($user_id, 'primary_blog', true) ) {
		update_user_meta($user_id, 'primary_blog', $blog_id);
		$site = get_site( $blog_id );
		update_user_meta( $user_id, '源代码 _domain', $site->domain );
	}

	$user->set_role($role);

	/**
	 * Fires immediately after a user is added to a site.
	 *
	 * @since MU
	 *
	 * @param int    $user_id User ID.
	 * @param string $role    User role.
	 * @param int    $blog_id Blog ID.
	 */
	do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
	wp_cache_delete( $user_id, 'users' );
	wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
	restore_current_blog();
	return true;
}

更新日志

Version 描述
MU 1.0 Introduced.

相关函数

Uses

  • wp-includes/ms-blogs.php:
    get_site()
  • wp-includes/cache.php:
    wp_cache_delete()
  • wp-includes/l10n.php:
    __()
  • wp-includes/pluggable.php:
    get_userdata()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/user.php:
    get_user_meta()
  • wp-includes/user.php:
    update_user_meta()
  • wp-includes/ms-functions.php:
    add_user_to_blog
  • wp-includes/ms-blogs.php:
    switch_to_blog()
  • wp-includes/ms-blogs.php:
    restore_current_blog()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 6 more uses
    Hide more uses


Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php:
    WP_REST_Users_Controller::create_item()
  • wp-includes/ms-functions.php:
    add_existing_user_to_blog()
  • wp-includes/ms-functions.php:
    add_new_user_to_blog()
  • wp-includes/ms-functions.php:
    wpmu_create_blog()
  • wp-includes/ms-functions.php:
    get_active_blog_for_user()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    
    <?php
    //ADD USER ID 1 TO BLOG ID 1 AS AN EDITOR
    $user_id = 1; 
    $blog_id = 1;
    $role = 'editor';
    add_user_to_blog( $blog_id, $user_id, $role )
    ?>
    
    <?php 
    //ADD USER ID 2 TO BLOG ID 3 AS AN ADMINISTRATOR
    $user_id = 2; 
    $blog_id = 3;
    $role = 'administrator';
    add_user_to_blog( $blog_id, $user_id, $role )
    ?>
    

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

热门推荐

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

反馈