Function and usage of remove_user_from_blog() in wordpress

Question

Answers ( 1 )

    0
    2024-01-07T23:06:14+00:00

    The remove_user_from_blog() function in WordPress is used to remove a user from a specific blog. This function is particularly useful in a multisite WordPress setup where a user might be a member of multiple blogs.

    Function Signature

    remove_user_from_blog( int $user_id, int $blog_id, int $reassign ): true|WP_Error
    

    Description

    • Purpose: The function removes a user from a blog and, if specified, reassigns their posts to another user.
    • Action Hook: It triggers the remove_user_from_blog action, allowing additional actions to be performed when a user is removed.
    • Reassignment Feature: The $reassign parameter lets you assign the removed user's posts to another user.

    Parameters

    1. $user_id (int, required): The ID of the user you want to remove. This is a mandatory parameter.
    2. $blog_id (int, optional): The ID of the blog from which the user is being removed. If not specified, the default value is 0.
    3. $reassign (int, optional): The ID of the user to whom you want to reassign the removed user's posts. If not specified, the default is 0, indicating no reassignment.

    Return Value

    • The function returns true on successful removal of the user.
    • If the user doesn't exist, it returns a WP_Error object.

    Sample Usage

    Here's an example of how remove_user_from_blog() can be used:

    $user_id = 123; // ID of the user to remove
    $blog_id = 456; // ID of the blog
    $reassign_id = 789; // ID of the user to whom posts will be reassigned
    
    $result = remove_user_from_blog($user_id, $blog_id, $reassign_id);
    
    if ( is_wp_error( $result ) ) {
        // Handle error
        echo 'Error: ' . $result->get_error_message();
    } else {
        // Success
        echo 'User removed successfully.';
    }
    

    In this example, the user with ID 123 is removed from the blog with ID 456. Their posts are reassigned to the user with ID 789. The function checks if the operation was successful and outputs an appropriate message.

Leave an answer