Skip to main content
code formatting
Source Link

The new fields for the profile are in functions.php with the following code:

<!-- ROLE -->

<?php $role = get_user_meta($user->ID, 'member_role', true); ?>

<table class="form-table">

<tr>
    <th><label for="member_role">Lab Role</label></th>
    <td>
        <select name="member_role" id="member_role">
        <option value="" <?php if($role == ''){echo('selected="selected"');}?>>Choose role</option>
        <option value="principal-investigator" <?php if($role == 'principal-investigator'){echo('selected="selected"');}?>>Principal Investigator</option>
        <option value="labmanager" <?php if($role == 'labmanager'){echo('selected="selected"');}?>>Lab Manager</option>
        <option value="administrativeassistant" <?php if($role == 'administrativeassistant'){echo('selected="selected"');}?>>Administrative Assistant</option>
        <option value="postdoc" <?php if($role == 'postdoc'){echo('selected="selected"');}?>>Postdoctoral Fellow</option>
        <option value="gradstudent" <?php if($role == 'gradstudent'){echo('selected="selected"');}?>>Graduate Student</option>
        <option value="researchtech" <?php if($role == 'researchtech'){echo('selected="selected"');}?>>Research Technician</option>
        <option value="undergradstudent" <?php if($role == 'undergradstudent'){echo('selected="selected"');}?>>Undergraduate Student</option>
        <option value="labsupport" <?php if($role == 'labsupport'){echo('selected="selected"');}?>>Lab Support</option>
        </select>
        <br />
        <span class="description">Please select your role at the lab.</span>
    </td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
add_action('user_register', 'my_save_extra_profile_fields');
add_action('profile_update', 'my_save_extra_profile_fields');

function my_save_extra_profile_fields( $user_id ) {

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

update_usermeta( absint( $user_id ), 'degrees_and_affiliations', wp_kses_post( $_POST['degrees_and_affiliations'] ) );
update_usermeta( absint( $user_id ), 'member_role', wp_kses_post( $_POST['member_role'] ) );

$items = array('principal-investigator', 'labmanager', 'administrativeassistant', 'postdoc',  'gradstudent', 'researchtech', 'undergradstudent', 'labsupport' );
$role = get_user_meta($user_id, 'member_role', true);
$order = array_search($role, $items);

update_user_meta( absint( $user_id ), 'lab_member_order', $order);
}

Then the code that shows the users in the page is as follows:

$results = get_users(); 

    foreach ($results as $result) {

        // Get data about each user as an object
        $user = get_userdata($result->ID); 


        // Create a flat array with only the fields we need
        $directors[$user->ID] = array(
            'dir_order'     =>  $user->menu_order,
            'dir_id'        =>  $user->ID,
            'dir_name'      =>  $user->first_name.' '.$user->last_name,    
            'dir_email'     =>  $user->user_email,
        );
    }

    // Sort
    sort($directors); 

    // The list
    echo '<ul id="rightcolumndirector">';

    // For each result


    foreach ($directors as $director) { 

        // Set up the variables
        $dir_id = $director['dir_id'];
        $dir_order = $director['dir_order'];
        $dir_name = $director['dir_name'];
        $dir_email = $director['dir_email'];
        $dir_link = get_bloginfo('home').'/?author='.$director['dir_id']; 
        $dir_status = get_field('alumni', 'user_'.$dir_id);
if ($dir_status == 0 && $dir_id !== 24) { ?>
        <div class="author-nucleus">
            <a href="<?php echo get_author_posts_url( $dir_id ); ?>">
                <div class="author-avatar">
                    <div class="hexa">
                        <div class="hex1">
                            <div class="hex2">
                                <?php echo get_wp_user_avatar( $dir_email, 'large' ); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
                <div class="author-info">
                    <h2>
                        <a class="author-name" href="<?php echo get_author_posts_url( $dir_id ); ?>">
                            <?php echo $dir_name; ?>
                        </a><?php
                                if($dir_email != '')
                                {
                                    printf('<a href="mailto:%s">%s</a>', $dir_email, '<span class="dashicons dashicons-email-alt"></span>');
                                }
                            ?>
                    </h2>
                    <hr />
                    <?php 
                        get_member_role($dir_id);
                    ?>
                    <ul class="nucleus-icons-test">
                        <li>
                            <div>
                            <img src="<?php $user_icon = get_field('user_icon', 'user_'.$dir_id); 
                                    echo $user_icon['url']; ?>" />
                            <span><?php echo $dir_name; ?></span>
                            </div>
                        </li>
                    <?php   
                        get_subjects($dir_id, 'post', 4);
                    ?>
                    </ul>
                </div>
            </div>

<?php

}
    } 
    ?>

The new fields for the profile are in functions.php with the following code:

<!-- ROLE -->

<?php $role = get_user_meta($user->ID, 'member_role', true); ?>

<table class="form-table">

<tr>
    <th><label for="member_role">Lab Role</label></th>
    <td>
        <select name="member_role" id="member_role">
        <option value="" <?php if($role == ''){echo('selected="selected"');}?>>Choose role</option>
        <option value="principal-investigator" <?php if($role == 'principal-investigator'){echo('selected="selected"');}?>>Principal Investigator</option>
        <option value="labmanager" <?php if($role == 'labmanager'){echo('selected="selected"');}?>>Lab Manager</option>
        <option value="administrativeassistant" <?php if($role == 'administrativeassistant'){echo('selected="selected"');}?>>Administrative Assistant</option>
        <option value="postdoc" <?php if($role == 'postdoc'){echo('selected="selected"');}?>>Postdoctoral Fellow</option>
        <option value="gradstudent" <?php if($role == 'gradstudent'){echo('selected="selected"');}?>>Graduate Student</option>
        <option value="researchtech" <?php if($role == 'researchtech'){echo('selected="selected"');}?>>Research Technician</option>
        <option value="undergradstudent" <?php if($role == 'undergradstudent'){echo('selected="selected"');}?>>Undergraduate Student</option>
        <option value="labsupport" <?php if($role == 'labsupport'){echo('selected="selected"');}?>>Lab Support</option>
        </select>
        <br />
        <span class="description">Please select your role at the lab.</span>
    </td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
add_action('user_register', 'my_save_extra_profile_fields');
add_action('profile_update', 'my_save_extra_profile_fields');

function my_save_extra_profile_fields( $user_id ) {

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

update_usermeta( absint( $user_id ), 'degrees_and_affiliations', wp_kses_post( $_POST['degrees_and_affiliations'] ) );
update_usermeta( absint( $user_id ), 'member_role', wp_kses_post( $_POST['member_role'] ) );

$items = array('principal-investigator', 'labmanager', 'administrativeassistant', 'postdoc',  'gradstudent', 'researchtech', 'undergradstudent', 'labsupport' );
$role = get_user_meta($user_id, 'member_role', true);
$order = array_search($role, $items);

update_user_meta( absint( $user_id ), 'lab_member_order', $order);
}

Then the code that shows the users in the page is as follows:

$results = get_users(); 

    foreach ($results as $result) {

        // Get data about each user as an object
        $user = get_userdata($result->ID); 


        // Create a flat array with only the fields we need
        $directors[$user->ID] = array(
            'dir_order'     =>  $user->menu_order,
            'dir_id'        =>  $user->ID,
            'dir_name'      =>  $user->first_name.' '.$user->last_name,    
            'dir_email'     =>  $user->user_email,
        );
    }

    // Sort
    sort($directors); 

    // The list
    echo '<ul id="rightcolumndirector">';

    // For each result


    foreach ($directors as $director) { 

        // Set up the variables
        $dir_id = $director['dir_id'];
        $dir_order = $director['dir_order'];
        $dir_name = $director['dir_name'];
        $dir_email = $director['dir_email'];
        $dir_link = get_bloginfo('home').'/?author='.$director['dir_id']; 
        $dir_status = get_field('alumni', 'user_'.$dir_id);
if ($dir_status == 0 && $dir_id !== 24) { ?>
        <div class="author-nucleus">
            <a href="<?php echo get_author_posts_url( $dir_id ); ?>">
                <div class="author-avatar">
                    <div class="hexa">
                        <div class="hex1">
                            <div class="hex2">
                                <?php echo get_wp_user_avatar( $dir_email, 'large' ); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
                <div class="author-info">
                    <h2>
                        <a class="author-name" href="<?php echo get_author_posts_url( $dir_id ); ?>">
                            <?php echo $dir_name; ?>
                        </a><?php
                                if($dir_email != '')
                                {
                                    printf('<a href="mailto:%s">%s</a>', $dir_email, '<span class="dashicons dashicons-email-alt"></span>');
                                }
                            ?>
                    </h2>
                    <hr />
                    <?php 
                        get_member_role($dir_id);
                    ?>
                    <ul class="nucleus-icons-test">
                        <li>
                            <div>
                            <img src="<?php $user_icon = get_field('user_icon', 'user_'.$dir_id); 
                                    echo $user_icon['url']; ?>" />
                            <span><?php echo $dir_name; ?></span>
                            </div>
                        </li>
                    <?php   
                        get_subjects($dir_id, 'post', 4);
                    ?>
                    </ul>
                </div>
            </div>

<?php

}
    } 
    ?>
Source Link

Order users by user role

I have custom fields in the user profile that displays in the team page. It reads "director" "researcher" "graduate" "intern" and some others. When adding a new team member, you pick from a select box with the options.

Right now the page displays the users in date of creation order but I need to show them in hierarchy order (all directors first, then the researchers, then graduate, etc, etc).