In WordPress, you may need to create custom user roles for specific groups, such as VIP members, to provide exclusive features and recognition.
While you cannot add new roles directly from the WordPress admin, you can use the powerful Roles and Capabilities API to create them programmatically.
Creating a Custom Role (Example: VIP Member)
The following code example creates a 'VIP Member' role. It is based on the 'Subscriber' role's capabilities, with an additional custom capability added.
/**
* Register a custom user role: VIP Member
*/
function register_vip_role() {
// Get the Subscriber role's capabilities as a base
$subscriber = get_role( 'subscriber' );
if ( $subscriber ) {
$capabilities = $subscriber->capabilities;
} else {
// Fallback if Subscriber role doesn't exist
$capabilities = array( 'read' => true );
}
// Add the new role with the slug 'vip' and display name 'VIP Member'
add_role( 'vip', 'VIP Member', $capabilities );
// Get the new role object to add a custom capability
$vip_role = get_role( 'vip' );
if ( $vip_role ) {
// Add a custom capability for accessing VIP-only content
$vip_role->add_cap( 'read_vip_content' );
}
}
// Hook the function to WordPress initialization
add_action( 'init', 'register_vip_role' );
How to Use
Add the code above to the end of your active theme's functions.php file. After saving, refresh your WordPress admin area. Go to Users → Add New or All Users, and you will see the new 'VIP Member' option in the Role dropdown.
Displaying Different Content for Different Roles
After creating the role, you can use conditional checks in your theme templates to show different content. For example, to display content only to users with the read_vip_content capability:
// Example in a theme template file (e.g., single.php)
if ( current_user_can( 'read_vip_content' ) ) {
// Display VIP-only content
echo '
';
} else {
// Display a message for non-VIP users
echo '
This content is reserved for VIP members. Please upgrade your account.
';
}
Important Note: Before modifying theme files, it is recommended to create a child theme or make a backup. Direct edits can be lost during theme updates. For production sites, managing custom roles and capabilities via a dedicated functionality plugin is the best practice.