I have created some custom user field , so i can manage my users as dealer in my website . Now my problem is with that user role , i want to make something in my function.php to check if my custom fields are not empty so the user role can change to Dealer ,otherwise let be a simple user .

I hope someone can help .

function fb_add_custom_user_profile_fields( $user ) { ?>

<h3 id="dealer_button" ><?php _e(' DEALER SECTION ', 'racecar.ch'); ?></h3>
<div id="dealerpackage"></div>

<table class="form-table">
            <label for="address"><?php _e('NR. OF FEATURED PACKAGE ', 'racecar.ch'); ?>
            <input type="text" name="feature_package" id="feature_package" value="<?php echo esc_attr( get_the_author_meta( 'feature_package', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter number of featured packages.', 'Racecar.ch'); ?></span>

            <label for="address"><?php _e('NR. OF PREMIUM PACKAGE ', 'racecar.ch'); ?>
            <input type="text" name="premium_package" id="premium_package" value="<?php echo esc_attr( get_the_author_meta( 'premium_package', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter number of premium packages.', 'Racecar.ch'); ?></span>

    <!-- <tr>
            <label for="address"><?php _e(' Dealer Contact URL  ', 'racecar.ch'); ?>
            <input type="text" name="dealer_url" id="dealer_url" value="<?php echo esc_attr( get_the_author_meta( 'dealer_url', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter the url of contact form .', 'Racecar.ch'); ?></span>
    </tr> -->

   function fb_save_custom_user_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return FALSE;

       update_usermeta( $user_id, 'feature_package', $_POST['feature_package'] );
       update_usermeta( $user_id, 'premium_package', $_POST['premium_package'] );
       //update_usermeta( $user_id, 'dealer_url', $_POST['dealer_url'] );


add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields',100 );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields',100);

add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields',100 );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields',100 );

Please edit your function and add code used below for adding user role in functions.php

This is basic idea how roles can be added and removed and how you can check your filed values. Please use according to your requirements.

add_action( 'profile_update', 'my_profile_update', 10, 2 );

function my_profile_update( $user_id, $old_user_data ) {

// here you need to update your required conditions, ie: check if meta values are set or not. I haven't checked your meta values but its supposed to like condition which is written in if condition, otherwise thr role update will work but take care of your condition

  if(get_the_author_meta( "feature_package", $user_id ) && get_the_author_meta( "premium_package", $user_id )){
     $u = new WP_User( $user_id );
     $u->set_role('author'); // please replace with your required role ie: delar 
     $u = new WP_User( $user_id );

WP _User class

For more details please follow links below:

profile_update developer

profile_update codex

Just for explanation, what above code is going to do. When any user will update his profile. The condition will check if there are values set in user custom fields, then the role of user will get changed according to passed value like "author" in this example.


| improve this answer | |
  • Thank you Jas four your reply , i tried your solution but it seems not working for me //I think this is how it can work for your case if(get_the_author_meta( "feature_package", $user_id ) && get_the_author_meta( "premium_package", $user_id )){ $u = new WP_User( $user_id ); $u->remove_role( 'individuals' ); // remove previous role $u->add_role( 'professional' ); // set your delar role } – Roland Allla Nov 23 '15 at 16:38
  • I have updated answer, please check thats working for me. – jas Nov 23 '15 at 16:56
  • Great , Thank you,is working for me now this solution .:) .I was thinking to make something different , is there any way that i can check all users , and to change the role ,only to them Who have one of this filed fulfilled .and if both field are empty the user role must change to 'subscriber' . Thank you for your time – Roland Allla Nov 24 '15 at 8:35
  • Above will work if when user will edit there profile. But I think if you want to do in bulk with users for once, there will be need of script in which you can fetch all users and check required fields with above condition. And can modify if fields are not empty then assign particular role otherwise assign other particular role. – jas Nov 24 '15 at 8:46
  • yes your example is working if they edit they profile .at is all right . But in my case it will be better if i check all users as you said , I don't have to much experience with bulk .I will appreciate your help – Roland Allla Nov 24 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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