Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to give new users two specific user role options upon WordPress user registration

see register_form action hook for modifying the registration form

add_action( "register_form", function() {
    $html="<p>".__('Do you want to buy or sell?<br>Choose your role').'</p>';
    $html .= '<p>';
        $html .= '<label for="_customer"><input type="radio" name="user_role" id="_customer" value="customer"/>'.__('Customer').'</label><br>';
        $html .= '<label for="_vendor"><input type="radio" name="user_role" id="_vendor" value="_vendor"/>'.__('Customer').'</label>';
    $html .= '</p>';
    echo $html;
} );

you can use user_register action to save selected user role.

add_action( "user_register", function( $user_id ) {
    $allowed = array( 'customer', 'vendor' );
    if( isset( $_POST['YourFieldName'] ) && in_array( $_POST['YourFieldName'], $allowed ) ){
        $user = new WP_User( $user_id );
        $user->set_role($_POST['YourFieldName']);
    }
} );

check WP_User class reference document for more information;

Related Posts:

  1. How build a custom login/register form with error handling?
  2. wordpress separate registration for different roles
  3. Remove username in emails or swap username for email
  4. WordPress shows registration link for non logged users
  5. Is it necessary to sanitize wp_set_password user input?
  6. User management system similar to wordpress one?
  7. Issues adding Recaptcha v3 to WordPress Registration
  8. Newbie question. Login/Registration. New PHP page
  9. How do I do so that people can register on my wordpress site?
  10. Paypal form integration with wordpress registration form without plugin [closed]
  11. Do not execute If User is login as Administrator or any Specific Role
  12. Problem with login form
  13. Login to wordpress by clicking a link and specifying usernaname and password in url
  14. Log in / Log Out Custom Button
  15. Should `wp_login` be used since it’s deprecated?
  16. How to redirect users based on role and content of redirect_to?
  17. Change CSS based on is_user_logged_in
  18. How to display login form anywhere, when user isn’t logged in, without redirecting?
  19. How can I open up my administrative panel to everyone?
  20. User register hook is not working in woocomerce register form
  21. Password minimum length in personal subscription [closed]
  22. Run a code only on theme activation only during first activation
  23. Add post with approval by the admin
  24. Fatal error: Call to undefined function register_new_user()
  25. How Can I Edit the Registration Code?
  26. How do you create two separate Register pages?
  27. How to keep the capability of users and disable Gutenberg editor in WordPress?
  28. How can I add a new row in a separate database when someone registers via WordPress?
  29. Shortcode for Listing Users from Meta Value?
  30. One account with multiple logins
  31. Array to string conversion error in PHP 7.2 when returning user role as class
  32. Lost in trying to create user database system
  33. Custom Registration username_exists / email_exists
  34. Shortcode to log user into current URL
  35. Noindex subscriber author page
  36. PHP getting error when trying to access WP-Admin Dashboard
  37. I installed WordPress locally now how do I login?
  38. If user is logged in not working
  39. Custom User Registration script only allowing usernames with 16 characters
  40. Change homepage content if user is logged in – BuddyPress
  41. Query for user roles
  42. Show login greeting above sub-menu links?
  43. How can I call a specific file (via php) by referencing the logged-in username?
  44. is_user_logged_in returning nothing on custom page
  45. Plugin: Front-end Editor – User role specific editable content?
  46. Help with accessing wp-admin page and resolving error messages
  47. Execute PHP code only with specific user role
  48. Edit and delete permissions pages and posts
  49. How to Add a cutsom slug to my custom author role
  50. Execute function only for specific user roles
  51. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  52. Can’t log in to WordPress wp-admin after adding code to functions.php
  53. Allow a user or role to view drafts and previews, but not other admin privileges?
  54. Check if user had autologin & if so, logout
  55. Admin Panel 404 Error after login
  56. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  57. How to block specific user id in custom login form?
  58. Log out without confirmation request (nonce)
  59. Custom User registration system
  60. how to create a user rule that can only manage support tickets?
  61. Hide payment method based on user role AND minimum cart value
  62. Having trouble creating two shortcodes, one for logged in user and one for visitors
  63. Call WP Rest-Api to GET /users/me returned NOTHING in console
  64. Uploading/integrating custom user registration page
  65. Edit a function to take different actions based on user role
  66. Restrict wordpress access to logged users only
  67. Infinite loop when logging out using custom login form
  68. Menu not updating for logged in users after redirect
  69. Including user data in “new user notification email”
  70. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  71. Used a code to revoke some dashboard menus for my contributors but for some reason it’s revoked me access to the editor [closed]
  72. Display specific page if user signed in
  73. Change Login or Logout text based on status
  74. Refresh page after login with litespeed cache
  75. How to give custom roles the capability to edit one Menu instead of every Menu
  76. Help hooking into user_register
  77. How to auto-generate random numbers in username?
  78. Redirect after login depending on the URL
  79. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  80. Redirecting the lost password page request when using a custon login page
  81. How do I send a POST request with params with WordPress REST API
  82. wp-login.php?redirect_to=https problem
  83. Add another role to a user when they click a button?
  84. Obtain wordpress user role
  85. is_user_logged_in() not working in homepage
  86. Attaching an upload to a user
  87. How to properly create multiple conditions to redirect users roles to different pages
  88. Echo Option Value Based On WordPress User Role
  89. Creating a custom register form
  90. Check if a user is logged into my WordPress site which is on a different server
  91. Role exception for a settings sub menu
  92. Select dropdown with 2 choices from foreach
  93. Adding a sidebar to wp-login.php
  94. Allow BBPress participant role to trash topics
  95. List users in a dropdown for login
  96. Custom user roles doesn’t apply changes
  97. Registration form not registering First and Last name
  98. Include administrator in author list
  99. Not logged in when using http
  100. add bootstrap modal after login in wordpress
Categories PHP Tags login, php, user-registration, user-roles
How to add filter the post thumbnail before save in database?
Upload file type not permitted even if explicitly registered as upload_filetypes in the db (multisite)

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress