Back to Top

How to create a custom role and user globally for WordPress Multisite.

Updated 3 July 2024

What is WordPress Multisite

WordPress Multisite is a type of WordPress installation that allows you to create and manage a network of multiple websites from a single WordPress dashboard. This lets you easily make changes and keep all of your websites updated from one place.

What are the roles in WordPress

WordPress has six predefined roles: Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. Each role is allowed to perform a set of tasks called Capabilities. That means capabilities for each type of role are different.

Why do we need to create a custom role in WordPress..?

If you want to create a user with limited capabilities then you can create a custom role and assign this role to the same capabilities that you want.

WordPress provides five functions for managing WordPress roles and capabilities:

  • add_role(): For adding a custom role.
  • remove_role(): For removing a custom role.
  • add_cap(): For adding a custom capability to a role.
  • remove_cap(): For removing a custom capability from a role.
  • get_role (): Gets information about the role and its capabilities.

How to create a custom role globally..?

function wk_add_new_roles() {

	global $wpdb;

	add_role( 'wk_custom_role', esc_html__( 'Custom Role', 'wk-text-domain'), array(
			'read'                   => true,
			'level_0'                => true,
			'edit_posts'             => true,
			'publish_posts'          => true,
			'edit_published_posts'   => true,
			'edit_others_posts'      => true,
			'edit_private_posts'     => true,
		)
	);

	$main_user_roles = get_option( $wpdb->prefix . 'user_roles' );

	// Get all store using(get_sites()) this method 
	$blogs = get_sites();

	foreach ( $blogs as $key => $blog ) {

		// Skip current store as it has allready created above
		if( get_current_blog_id() !== $blog->blog_id ) {

			switch_to_blog( $blog->blog_id );

			$user_roles = get_option( $wpdb->prefix . 'user_roles' );

			if( ! empty( $main_user_roles[$role] ) ) {

				$user_roles[$role] = $main_user_roles[$role];

				update_option( $wpdb->prefix . 'user_roles', $user_roles );
			}
			restore_current_blog();
		}
	}
}

register_activation_hook( __FILE__, 'wk_add_new_roles' );

The results are shown here, and they will also be posted on your subsite.

Searching for an experienced
WordPress Company ?
Find out More
user_role

Add a user globally when a new user is created.

/**
 * Set new user to all subsites.
 *
 * @param int $user_id
 *
 * @param object $userdata
 *
 * @return void
 */
function wk_add_new_user( $user_id, $userdata ) {

	$blogs = get_sites();

	$user = get_userdata( $user_id );

	$status = false;

	if( in_array( 'wk_custom_role', $user->roles ) ) {
		$status = true;
	} elseif( isset( $_POST['role'] ) && $_POST['role'] === 'wk_custom_role' ) {
		$status = true;
	}

	if( $blogs && $status ) {
		foreach ( $blogs as $key => $blog ) {
			add_user_to_blog( $blog->blog_id, $user_id, 'wk_custom_role' );
		}
	}
}
add_action( 'user_register', 'wk_add_new_user' ,10 ,2 ); // For add new user
add_action( 'profile_update', 'wk_add_new_user' ,10 ,2 ); // For update user

Using this user will be added to all your subsites as well.

This is all about creating user and user custom roles for all your subsite.

Hope you like this article, Will meet you again with a new topic :). Also, visit our quality WooCommerce extensions.

Support

For any kind of technical assistance, please raise a ticket or send us a mail at [email protected]

You may also check out our exclusive WooCommerce Addons and can also explore our WooCommerce Development Services.

Thanks for Your Time! Have a Good Day!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

How to create a custom role and user globally for WordPress Multisite.