Filter uncanny-automator-pro

automator_pro_wpmu_add_user_to_blog_role

Filters the user role assigned when adding a user to a WordPress Multisite blog.

add_filter( 'automator_pro_wpmu_add_user_to_blog_role', $callback, 10, 3 );

Description

Filters the role assigned when adding a user to a WordPress Multisite blog. Developers can modify the role, the user object, or the action instance. This hook fires just before the user is added to the blog, allowing for dynamic role assignment based on custom logic.


Usage

add_filter( 'automator_pro_wpmu_add_user_to_blog_role', 'your_function_name', 10, 3 );

Parameters

$role (mixed)
This parameter contains the role that will be assigned to the user.
$user (mixed)
This parameter contains the role that will be assigned to the user on the blog.
$this (mixed)
This parameter holds information about the user being added to the blog.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'automator_pro_wpmu_add_user_to_blog_role' filter hook.
 *
 * This filter allows you to modify the role assigned to a user when they are added to a subsite
 * via Uncanny Automator Pro's "Add user to site" action.
 *
 * In this example, we check if the user is a subscriber and, if so, assign them the 'editor' role
 * instead of the default.
 */
add_filter( 'automator_pro_wpmu_add_user_to_blog_role', function( $role, $user, $automator_action_instance ) {

	// Ensure the $user object is valid and has a roles property
	if ( ! is_a( $user, 'WP_User' ) || empty( $user->roles ) ) {
		return $role; // Return the original role if user object is invalid
	}

	// Check if the user's current role is 'subscriber'
	if ( in_array( 'subscriber', $user->roles, true ) ) {
		// Assign the 'editor' role instead of the default
		return 'editor';
	}

	// Return the original role if the condition is not met
	return $role;

}, 10, 3 ); // 10 is the priority, 3 is the number of arguments accepted by the callback function

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/wpmu/actions/wpmu-add-specific-user-to-blog.php:115
uncanny-automator-pro/src/integrations/wpmu/actions/wpmu-add-user-to-blog.php:84

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
		// Get User ID
		$user_id = isset( $parsed['WPUSERID'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed['WPUSERID'] ) ) : 0;
		// Get Subsite ID
		$subsite_id = isset( $parsed['SUBSITEID'] ) ? absint( wp_strip_all_tags( $parsed['SUBSITEID'] ) ) : 0;

		$validate = get_site( $subsite_id );
		if ( 0 === $subsite_id || ! $validate instanceof WP_Site ) {
			$action_data['complete_with_errors'] = true;
			$error_message                       = sprintf( '%s: %s', __( "Subsite doesn't exist. Subsite ID", 'uncanny-automator-pro' ), $subsite_id );
			Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		$user = $this->get_user( $user_id );
		if ( ! $user instanceof WP_User ) {
			$action_data['complete_with_errors'] = true;
			$error_message                       = sprintf( '%s: %s', __( "User doesn't exist. User ID", 'uncanny-automator-pro' ), $user_id );
			Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		$user_id = $user->ID;
		$role    = get_option( 'default_role', 'subscriber' );
		if ( 1 === count( $user->roles ) ) {
			// IF the current user has only role, use that instead
			$role = $user->roles[0];
		}
		$role = apply_filters( 'automator_pro_wpmu_add_user_to_blog_role', $role, $user, $this );

		$r = add_user_to_blog( $subsite_id, $user_id, $role );

		if ( is_wp_error( $r ) ) {

			$action_data['complete_with_errors'] = true;
			Automator()->complete->action( $user_id, $action_data, $recipe_id, $r->get_error_message() );

			return;
		}

		Automator()->complete->action( $user_id, $action_data, $recipe_id );
	}

Scroll to Top