I have sucessfully integrated this code in functions.php

//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {

global $wp_roles;

echo '<label for="role">Type de client</label>';
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
   // Exclude default roles such as administrator etc. Add your own
   if ( in_array( $value['name'], [ 'Client', 'Entreprise'] )) {
      echo '<option value="'.$key.'">'.$value['name'].'</option>';
echo '</select>';

//2. Add validation.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
     $errors->add( 'role_error', __( '<strong>ERROR</strong>: You must include a role.', 'mydomain' ) );

return $errors;

//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {

$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );

and it works as expected on default WP registration page.

The theme I use for my projet has a custom modal login/signup window and I would like to have this code working in this window as well.

See default registration window : https://capricesdelectables.com/wp-login.php?action=register

See custom login box : https://capricesdelectables.com (click on "Connexion" top right red link)

This modal window is templated in a specific mytheme-login.php file

How should I call the code from functions.php to make that displayed in this modal window ?


1 Answer 1


Since this is custom functionality for a specific theme, you would either need to use custom hooks that the theme author included in the markup for that modal (unlikely), or contact the theme author directly to ask, or create a child theme so you can edit that file without it being overriden when the theme is updated. I would go the child theme route: https://codex.wordpress.org/Child_Themes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.