I have added in my functions.php child theme some user meta and works no problem, but I need to display this data only inside a user profile with a specific user role named "rivenditore" and if is instead the role is "installatori" I need to display another table how can i do?

Following below is the code tried

add_action( 'show_user_profile', 'stel_extra_field_meta' );
add_action( 'edit_user_profile', 'stel_extra_field_meta' );

function stel_extra_field_meta( $user )  {

        <h3>STEL Dati Rivenditore</h3>

        <table class="form-table">
                <th><label for="location">Location</label></th>
                <td><input type="text" name="location" value="<?php echo esc_attr(get_the_author_meta( 'location', $user->ID )); ?>" class="regular-text" /></td>
                <th><label for="regione">Regione</label></th>
                <td><input type="text" name="regione" value="<?php echo esc_attr(get_the_author_meta( 'regione', $user->ID )); ?>" class="regular-text" /></td>
                <th><label for="tipo_rivenditore">Tipo di Rivenditore</label></th>
                <td><input type="text" name="tipo_rivenditore" value="<?php echo esc_attr(get_the_author_meta( 'tipo_rivenditore', $user->ID )); ?>" class="regular-text" /></td>

                <th><label for="service_type">Servizio Scelto</label></th>
                <?php $value = esc_attr(get_the_author_meta( 'service_type', $user->ID )); $service_type = preg_replace('/\|.*/', '', $value); ?>
                <td><input type="text" name="service_type" value="<?php echo $service_type; ?>" class="regular-text" /></td>

                <th><label for="phone">Phone</label></th>
                <td><input type="text" name="phone" value="<?php echo esc_attr(get_the_author_meta( 'phone', $user->ID )); ?>" class="regular-text" /></td>

get_the_author_meta( $field, $userID ); 

update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ); 

add_action( 'personal_options_update', 'stel_save_extra_field_meta' );
add_action( 'edit_user_profile_update', 'stel_save_extra_field_meta' );

function stel_save_extra_field_meta( $user_id )
    update_user_meta( $user_id,'location', $_POST['location']  );
    update_user_meta( $user_id,'service_type',  $_POST['service_type']  );
    update_user_meta( $user_id,'phone',  $_POST['phone']  );
    update_user_meta( $user_id,'regione',  $_POST['regione']  );
    update_user_meta( $user_id,'tipo_rivenditore',  $_POST['tipo_rivenditore']  );
  • 1
    Please edit your question and add the relevant part of the code here.
    – Christine Cooper
    Jan 1 '16 at 0:49
  • 1
    Please move the relevant parts of your code from that site to here. All relevant information should be part of the question. As is, if that link expires this question is meaningless.
    – s_ha_dum
    Jan 1 '16 at 1:03

In your author.php file, get the user metadata via get_user_meta(), then do an if statement of $user->roles == 'rivenditore', display the meta data as you desire, and then do an elseif $user->roles == 'installatori' statement with what you wish to display for users on other roles.

EDIT: Code example as requested:

// get author data
$queried_object = get_queried_object();

// set author ID
$author_id = $queried_object->ID;

// get author roles in array
$roles_arr = $queried_object->roles;

// get the meta data, I set it to 'location' just to show you how it works
$user_meta_location = get_user_meta($author_id, 'location', TRUE);

if ($roles_arr[0] == 'rivenditore') {
    // do stuff for rivenditore users
    echo $user_meta_location; 
} elseif ($roles_arr[0] == 'installatori') {
    // do other stuff for installatori users
    echo $user_meta_location; 

I haven't tested it but it should work. Note that I have no idea what the key is for your meta_data so please set that. I have added comments so everything should be readable for sure. Good luck!

  • hi Christine thanks for your quick reply please can you make an example of code i'm quite new to php and wordpress i understand your suggest but i'm in a little bit difficulty in implementation of that.
    – gnappo
    Jan 1 '16 at 10:17
  • Sure, please see updated answer with code...
    – Christine Cooper
    Jan 1 '16 at 12:01
  • thanks Christine now is more clear, last thing my meta-key are in my code like service_type or location, is there a way to group or display some together? thanks a lot
    – gnappo
    Jan 1 '16 at 13:26
  • now i have done this: // get the meta data, SET THE KEY $user_meta = get_user_meta($author_id, 'meta_data_key', TRUE); if ($roles_arr[0] == 'Rivenditore') { add_action( 'show_user_profile', 'stel_extra_field_meta' ); add_action( 'edit_user_profile', 'stel_extra_field_meta' ); function stel_extra_field_meta( $user ) { ?> // my table etc etc
    – gnappo
    Jan 1 '16 at 13:28
  • now i have done this: // get the meta data, SET THE KEY $user_meta = get_user_meta($author_id, 'meta_data_key', TRUE); if ($roles_arr[0] == 'Rivenditore') { add_action( 'show_user_profile', 'stel_extra_field_meta' ); add_action( 'edit_user_profile', 'stel_extra_field_meta' ); etc etc... i have copied all my function function stel_extra_field_meta( $user ) { ?>//table here } elseif ($roles_arr[0] == 'installatori') { // do other stuff for installatori users } my meta data key could also location 4 now to make a test but nothing appear in profile page
    – gnappo
    Jan 1 '16 at 13:47

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.