Code Snippet to Change the Default WordPress User Role

Pavel K Jan 7, 2025 User Roles
How can I make sure that new users who sign up for my website have limited access instead of full access?
What is the approach to modify the default user role assigned to new users in WordPress using the `add_filter` function on the `default_role` hook?
Andy answered Jan 7, 2025

Change Default WordPress User Role

Custom Code Solution

This code changes the default user role for new registrations from "subscriber" to any role you specify. Add this code to your theme's functions.php file or in a site-specific plugin:

add_filter('default_role', function($default_role) {
    return 'subscriber'; // Change 'subscriber' to your desired default role
});

Available WordPress default roles you can use:

  • administrator
  • editor
  • author
  • contributor
  • subscriber

Enhanced Solution with Role Verification

This extended version includes a safety check to ensure the role exists before setting it:

add_filter('default_role', function($default_role) {
    $new_default_role = 'subscriber'; // Change this to your desired role
    
    // Verify role exists before setting it
    if (get_role($new_default_role)) {
        return $new_default_role;
    }
    
    return $default_role; // Fallback to WP default if specified role doesn't exist
});

Where to Place the Code

  1. In your theme's functions.php file
  2. In a site-specific plugin
  3. Via a code snippets plugin

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Members - Complete user role management
  2. User Role Editor - Advanced role customization
  3. Code Snippets - To implement the code solution without editing theme files

Additional Notes

  • The code takes effect immediately after implementation
  • It only affects new user registrations
  • Existing users' roles remain unchanged
  • You can verify the current default role in WordPress admin under Settings → General → New User Default Role