This code enables you to very easily create custom user roles once added to your functions file.
If you want to make a membership site using code rather than plugins, this code should come in handy.
The code uses the add_role function built into WordPress which includes 3 basic parameters.
- role
- Display Name
- capabilities
add_role('role', 'Display Name',array( 'capabilities' ));
Here’s the most basic working example:
add_role('member', 'Member',array( 'read' => true ));
Now when you go to Users > Add New > Add New User, you’ll find the new role added to the drop down menu next to Role.
The code creates a new role named Member.
The Member role only allows users assigned this role to read posts.
You can easily add more capabilities like this:
add_role( 'member', 'Member',array( 'read' => true, 'edit_posts' => true, ) );
The code in this tutorial enables you to separate subscribers from members.
If you only want to create a new role for logged in members and restrict access to the Dashboard, you can do so with this code:
add_action( 'after_setup_theme','add_custom_role' ); function add_custom_role(){ add_role( 'member', 'Logged In Members',array( 'read' => true )); }
You can then wrap your content in shortcodes to restrict access to content for logged in members only or based on user role.
You can see the core add-role function in WordPress > wp-includes > capabilities.php.
Was This Tutorial Helpful?