Add role to specific form

I’m trying to use this function:

function add_custom_role_to_registration_form() {?>

    <input type="hidden" name="custom_role" value="my_role">

    <?php

}

add_action( 'register_form', 'add_custom_role_to_registration_form' );

But I cannot pass this custom_role to: 

function set_custom_role_on_registration( $user_id ) {

    $custom_role = isset( $_POST['custom_role'] ) ? $_POST['custom_role'] : '';

    $user = get_user_by( 'ID', $user_id );

    $user->remove_role( 'subscriber' );

    if ( $custom_role === 'my_role' ) {

        $user->add_role( 'my_role' );

    }else{

        $user->add_role( 'my_role_2' );

    }

    wp_insert_user($user);

}

add_action( 'user_register', 'set_custom_role_on_registration' );

The first part of adding the hidden field to the form is not working, I’m using the Hiveress registration form, do you know other way of passing the role on form, so when I´m registering the user I can choose which profile I will give him?

Regards

1 Like

Hi,

Sorry for the inconvenience, but customization is beyond our support scope - it includes fixing bugs and guidance about the available features Support Policy | HivePress

If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work https://fvrr.co/32e7LvY

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.