Users & Login PHP Beginner

Set the Role for New Users in Code

Lock the "New User Default Role" setting so new sign-ups always get the role you choose, even if someone changes the setting.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 3 lines
add_filter( 'pre_option_default_role', function (): string {
	return 'subscriber';
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

If the “New User Default Role” setting is ever changed to Editor or Administrator, by mistake or by an attacker, every new sign-up gets that power. This snippet fixes the role in code so the setting can’t be changed.

How it works

pre_option_default_role runs before WordPress reads the setting from the database. Returning a role name skips the database value completely, so the setting always says Subscriber.

Things to watch

  • Change 'subscriber' to another role, such as 'customer' for WooCommerce stores.
  • The setting on Settings → General still shows a dropdown, but changes have no effect.
  • Users created by admins through Users → Add New still get whichever role the admin picks.

How to test it

Go to Settings → General, change New User Default Role to Editor and save. Reload: it should still say Subscriber.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only