How to add user roles dropdown in registration and login woocommerce wordpress

2k Views Asked by At

I have a job site but i want to set drop down with roles i have 2 roles candidate and employer so i want to set a drop down select they roles i am using woocommerce registration and login i want to integrate dropdown with that roles for registration and login woocommerce.

function wp_dropdown_roles( $selected = '' ) {
    $p = '';
    $r = '';

    $editable_roles = array_reverse( get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role($details['name'] );
        if ( $selected == $role ) // preselect specified role
            $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
        else
            $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    }
    echo $p . $r;
}
1

There are 1 best solutions below

0
On BEST ANSWER

Try this :

function ts_wp_dropdown_roles( $selected = '' ) {
    $p = '';
    $r = '';

    $editable_roles = array_reverse( ts_get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role($details['name'] );
        if ( $selected == $role ) // preselect specified role
            $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
        else
            $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    }
    echo $p . $r;
}

// Custom editable_roles function to be used by "ts_wp_dropdown_roles" function
function ts_get_editable_roles() {
    global $wp_roles;

    $all_roles = $wp_roles->roles;

    /**
     * Filter the list of editable roles.
     *
     * @since 2.8.0
     *
     * @param array $all_roles List of roles.
     */
    $editable_roles = apply_filters( 'ts_editable_roles', $all_roles );

    return $editable_roles;
}

add_filter("ts_editable_roles" , "ts_filter_roles", 10 ,1);

function ts_filter_roles($all_roles){
    foreach($all_roles as $key=>$value){
        if($key !== "employer" && $key !== "candidate"){
            unset($all_roles[$key]);
        }
    }
    return $all_roles;

}