Snippet

Change Default Role for New Users in WordPress Easily

How to change default user role in wordpressSet default role for new wordpress usersChange default role during wordpress registrationWordpress change new user default roleCustomize default user role in wordpressWordpress set default role for new registrationsHow to assign default role to new users in wordpressChange default user role in wordpress multisiteWordpress default user role settingsProgrammatically set default user role wordpress

Explanation

If you want to change the default role for new users in WordPress, this code snippet is your go-to solution. It sets the default role to 'editor' for anyone who registers on your site.

Here's what you need to know:

  • This snippet changes the default role from 'subscriber' (the usual default) to 'editor'.
  • If you want a different role, just replace 'editor' with the role you prefer, like 'author' or 'contributor'.

For WordPress Multisite:

  • The second part of the code does the same thing but for a multisite setup. It ensures that new users across all sites in the network get the 'editor' role by default.
  • Again, you can change 'editor' to any role you need.

Remember, this change affects all new users, so choose a role that fits your site's needs. Happy customizing!

Code

1<?php 2/** 3 * Change the default user role for newly registered users in WordPress. 4 * 5 * This snippet sets the default user role to 'editor' for new registrations. 6 * Adjust the role as needed by replacing 'editor' with the desired role slug. 7 * 8 * @param string $default_role The default role slug. 9 * @return string The modified default role slug. 10 */ 11function wp_dudecom_set_default_user_role( $default_role ) { 12 // Set the default role to 'editor'. Change 'editor' to your desired role slug. 13 return 'editor'; 14} 15add_filter( 'pre_option_default_role', 'wp_dudecom_set_default_user_role' ); 16 17/** 18 * Change the default user role for new users in a WordPress Multisite. 19 * 20 * This snippet sets the default user role to 'editor' for new registrations in a multisite setup. 21 * Adjust the role as needed by replacing 'editor' with the desired role slug. 22 * 23 * @param string $default_role The default role slug. 24 * @return string The modified default role slug. 25 */ 26function wp_dudecom_set_default_user_role_multisite( $default_role ) { 27 // Set the default role to 'editor'. Change 'editor' to your desired role slug. 28 return 'editor'; 29} 30add_filter( 'pre_site_option_default_user_role', 'wp_dudecom_set_default_user_role_multisite' ); 31?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have access to your WordPress site's file system, either via FTP or a file manager in your hosting control panel.
  • Have a basic understanding of WordPress roles and capabilities.

Implementation Steps:

  1. Access your WordPress site's file system using an FTP client or a file manager.
  2. Navigate to the directory of your active theme, usually found in wp-content/themes/your-theme-name/.
  3. Open the functions.php file in a text editor.
  4. Copy and paste the provided code snippet into the functions.php file. Ensure it is placed within the PHP tags.
  5. If you want to change the default role to something other than 'editor', replace 'editor' with your desired role slug (e.g., 'author', 'contributor').
  6. Save the changes to the functions.php file.
  7. If you are using a WordPress Multisite setup, ensure the second function is also included to apply the changes network-wide.
  8. Test the registration process to confirm that new users are assigned the correct default role.

If you need help with implementation or more advanced functionality, consider using the services of wp-dude.com.