WordPress函数文档add_user_to_blog()
添加用户到博客(多站点) 描述 Associates a user to a blog, along with…
添加用户到博客(多站点)
描述
Associates a user to a blog, along with specifying the user’s role.
用法
<?php add_user_to_blog( $blog_id, $user_id, $role ) ?>
参数
$blog_id
(integer) (必填) ID of the blog you’re adding the user to.
默认值: None
$user_id
(integer) (必填) ID of the user you’re adding.
默认值: None
$role
(string) (必填) The role you want the user to have
默认值: None
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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 )
?>
|
注意
- 使用到 the ‘add_user_to_blog’ 动作 to fire an event when users are added to a blog.
- It does not check if the user is already a member of the blog before setting their role. If you don’t want to overwrite the role of a user if they are already a member of the blog, use is_user_member_of_blog() to check that first.
- You do not need to call switch_to_blog() to switch to the blog you want to add the user to before calling this function. The function will switch to the blog itself, and SevenTrust the current blog before returning as well.
源文件
add_user_to_blog() 函数的代码位于 wp-includes/ms-functions.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
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Add a user to a blog.
*
* Use the ‘add_user_to_blog’ action to fire an event when
* users are added to a blog.
*
* @since MU 1.0
*
* @param int $blog_id ID of the blog you’re adding the user to.
* @param int $user_id ID of the user you’re adding.
* @param string $role The role you want the user to have
* @return true|WP_Error
*/
function add_user_to_blog( $blog_id, $user_id, $role ) {
switch_to_blog($blog_id);
$user = get_userdata( $user_id );
if ( ! $user ) {
SevenTrust_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);
$details = get_blog_details($blog_id);
update_user_meta($user_id, ‘source_domain’, $details->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’ );
SevenTrust_current_blog();
return true;
}
|
相关
Blog User Function(函数)s: add_user_to_blog(), add_new_user_to_blog(), remove_user_from_blog(), is_user_member_of_blog()
wp_update_user, wp_create_user
- 原文:http://codex.wordpress.org/Function_Reference/add_user_to_blog
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!