wc_modify_map_meta_cap( array $caps, string $cap, int $user_id, array $args )

Modify capabilities to prevent non-admin users editing admin users.


Description Description

$args[0] will be the user being edited in this case.


Parameters Parameters

$caps

(Required) Array of caps.

$cap

(Required) Name of the cap we are checking.

$user_id

(Required) ID of the user being checked against.

$args

(Required) Arguments.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/wc-user-functions.php

function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
	if ( is_multisite() && is_super_admin() ) {
		return $caps;
	}
	switch ( $cap ) {
		case 'edit_user':
		case 'remove_user':
		case 'promote_user':
		case 'delete_user':
			if ( ! isset( $args[0] ) || $args[0] === $user_id ) {
				break;
			} else {
				if ( ! wc_current_user_has_role( 'administrator' ) ) {
					if ( wc_user_has_role( $args[0], 'administrator' ) ) {
						$caps[] = 'do_not_allow';
					} elseif ( wc_current_user_has_role( 'shop_manager' ) ) {
						// Shop managers can only edit customer info.
						$userdata                    = get_userdata( $args[0] );
						$shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) );
						if ( property_exists( $userdata, 'roles' ) && ! empty( $userdata->roles ) && ! array_intersect( $userdata->roles, $shop_manager_editable_roles ) ) {
							$caps[] = 'do_not_allow';
						}
					}
				}
			}
			break;
	}
	return $caps;
}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.