The Roles for Membership Levels Add On creates a new WordPress User Role for each membership level you have created. This recipe performs a batch update on all members with the Subscriber role to their level’s custom role, ignoring the Administrator role and any other roles you specify to skip.

The code is designed to be a one-time update script for sites that would like to use the Roles Add On on an existing site with members.

Banner Image for Bulk Update User Roles to a Custom Role Based on Membership Level

The Code Recipe

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

Update the array on line 28 with your list of roles to ignore. The script will skip all users with these roles. Be sure to keep administrator in the $ignored_roles array or you will overwrite your own admin account and lose access to your site. This recipe is designed to be used in combination with the Roles for Membership Levels Add On.

You must be logged in as the administrator to trigger the script to run. Navigate to your WordPress admin and add ?updateroles=LEVELID to the URL, replacing and re-running the script for each unique membership level ID you would like to run the roles update for.

https://domain.com/wp-admin/?updateroles=1

In the example URL above, change “domain.com” to your URL of your website. This will trigger the script to update the user roles.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics
Was this article helpful?
YesNo