|
/ Knowledge Base /Filters/ How to Add or Remove Users from Access Groups

How to Add or Remove Users from Access Groups

We’ve introduced two new functions in SureMembers to give you more control when managing user access. You can now add or remove users from Access Groups using the code below.

These functions are helpful if you want to handle access dynamically, like after a user registers, completes a purchase, or performs a specific action on your site.

suremembers_add_user_to_group( $user_id, $access_group_id );

This function adds a user to one or more access groups.

Example:

add_action( 'admin_footer', function() {
suremembers_add_user_to_group( 12, 145 );
// This adds user ID 12 to Access Group ID 145
});

To add the user to multiple access groups:

add_action( 'admin_footer', function() {
suremembers_add_user_to_group( 12, [145, 146, 147] );
});

suremembers_remove_user_from_group( $user_id, $access_group_id );

This function removes a user from one or more access groups.

Example:

add_action( 'admin_footer', function() {
suremembers_remove_user_from_group( 12, 145 );
// This removes user ID 12 from Access Group ID 145
});

To remove the user from multiple access groups:

add_action( 'admin_footer', function() {
suremembers_remove_user_from_group( 12, [145, 146, 147] );
});

Note:

  • In the examples above, we’re using the admin_footer action hook just for demonstration.
  • You should replace it with an action hook that matches when you want this to happen. For example:
    • After a user completes a form
    • After a WooCommerce order is marked complete
    • On user login/logout
  • Make sure your code is added in a custom plugin or a child theme’s functions.php.

If you’re unsure which hook to use, feel free to contact support or check WordPress documentation for action hooks related to your use case.

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
On this page

Remind me about SureMembers's HUGE Black Friday Sale

Scroll to Top