函数文档

add_existing_user_to_blog()

💡 云策文档标注

概述

add_existing_user_to_blog() 是一个 WordPress 函数,用于基于 maybe_add_existing_user_to_blog() 提供的详细信息将现有用户添加到当前博客。它不应直接调用,而是作为内部函数使用。

关键要点

  • 函数接受一个可选的 $details 参数,必须为数组,包含 user_id 和 role 键。
  • 内部调用 add_user_to_blog() 来执行添加操作,并触发 added_existing_user 钩子。
  • 返回 true 表示成功,WP_Error 表示失败,如果 $details 未提供则返回 void。
  • 此函数仅供信息参考,实际开发中应使用 add_user_to_blog()。

代码示例

function add_existing_user_to_blog( $details = false ) {
    if ( is_array( $details ) ) {
        $blog_id = get_current_blog_id();
        $result  = add_user_to_blog( $blog_id, $details['user_id'], $details['role'] );
        do_action( 'added_existing_user', $details['user_id'], $result );
        return $result;
    }
}

注意事项

  • 此函数由 maybe_add_existing_user_to_blog() 调用,开发者不应直接使用它。
  • 确保 $details 数组格式正确,否则函数可能不执行任何操作。

📄 原文内容

Adds a user to a blog based on details from maybe_add_existing_user_to_blog() .

Parameters

$detailsarray|falseoptional
User details. Must at least contain values for the keys listed below.

  • user_id int
    The ID of the user being added to the current blog.
  • role string
    The role to be assigned to the user.

Default:false

Return

true|WP_Error|void True on success or a WP_Error object if the user doesn’t exist or could not be added. Void if $details array was not provided.

More Information

This function is called by maybe_add_existing_user_to_blog() and should not be called directly. This page is for informational purposes only. Use add_user_to_blog().

Source

function add_existing_user_to_blog( $details = false ) {
	if ( is_array( $details ) ) {
		$blog_id = get_current_blog_id();
		$result  = add_user_to_blog( $blog_id, $details['user_id'], $details['role'] );

		/**
		 * Fires immediately after an existing user is added to a site.
		 *
		 * @since MU (3.0.0)
		 *
		 * @param int           $user_id User ID.
		 * @param true|WP_Error $result  True on success or a WP_Error object if the user doesn't exist
		 *                               or could not be added.
		 */
		do_action( 'added_existing_user', $details['user_id'], $result );

		return $result;
	}
}

Hooks

do_action( ‘added_existing_user’, int $user_id, true|WP_Error $result )

Fires immediately after an existing user is added to a site.

Changelog

Version Description
MU (3.0.0) Introduced.

User Contributed Notes

  1. Skip to note 2 content

    function add_existing_user_to_blog($details = false) defines a function where add_existing_user_to_blog is the function name. Here in ($details = false) this part, $details is the parameter. Parameters are like placeholders for values that you can pass to the function when you call it. And ‘= false’, like the default value, means that if no values are provided for $details, then by default false will be worked.

    if (is_array($details)) this function first checks if the provided information is in the form of an array and the right format.

    $blog_id = get_current_blog_id() in which blog page or post you’re currently working on. Here $blog_id is the variable, and get_current_blog_id() is the function name.

    In the $result variable, the function named add_user_to_blog is called. Here, $blog_id represents the specific blog post to which a user is being added, with the user identified by $details[‘user_id’]. Additionally, in $details[‘role’], the permission level assigned to the user for the specific blog post is specified.

    do_action(‘added_existing_user’, $details[‘user_id’], $result) here action name ‘added_existing_user’. The parameter $details[‘user_id’] designates the user who was added. And the $result variable stores whether the user was successfully assigned or not.

    In short, this function makes sure that when you want to add an existing user to a particular blog, it verifies the information, adds the user, notifies the system to the addition, and then reports back to you on whether or not everything went according to plan. It functions similarly to a useful tool for WordPress site user management.