Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-user-manager/6d845fd9bf0d6f0e2c665b1f9d38bd74 to your computer and use it in GitHub Desktop.
Save wp-user-manager/6d845fd9bf0d6f0e2c665b1f9d38bd74 to your computer and use it in GitHub Desktop.
WP User Manager - Conditional user role at registration based on a custom field
<?php
add_action( 'wpum_after_registration', 'wpum_conditional_role', 20, 2 );
function wpum_conditional_role( $user_id, $values ) {
$field_key = 'wpum_field_14'; // Change this to your field key
if ( ! isset( $values['register'][ $field_key ] ) ) {
return;
}
$value = $values['register'][ $field_key ];
if ( $value == 'food' ) { // Change logic to be what you need based on value
$user = new WP_User( $user_id );
$user->set_role( 'editor' ); // Change role
}
}
@wp-user-manager
Copy link
Author

Save this file to your /wp-content/mu-plugins/ directory (you might need to create the mu-plugins directory).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment