Role Based Profile Fields in WordPress

I recently set out to customize WordPress profile fields for a specific role. You may want to have custom fields for users, but have different fields for different roles. For example, Authors may need some additional contact information, while Subscribers need only some basic fields. The concern I had was wether or not an Admin would be able to pull up any profile and edit all of the fields. Here’s the solution I found.

Credit Where Credit Is Due

First off, I never would have solved this as quick, had the kind folks over at AppThemes.com not shared a great little php function for checking roles. I was able to quickly modify the function so that Admin users could edit anyone’s profile. And, while there are many other great uses for this function, I am going to focus on editing profiles.

A Working Example

In this example I am going to use the function to add a Title field for Authors. The Title field won’t appear under any other roles, while allowing Admins to still edit the Author’s Title field.

Check the Role of the User

You will need to add this PHP function to your functions.php file.

/**
* Checks if a particular user has a role.
* Returns true if a match was found.
*
* @param string $role Role name.
* @param int $user_id (Optional) The ID of a user. Defaults to the current user.
* @return bool
*/
function check_user_role( $role, $user_id = null ) {
  if (isset($_REQUEST['user_id']))
    $user_id = $_REQUEST['user_id'];
  if ( is_numeric( $user_id ) )
    $user = get_userdata( $user_id );
    else
    $user = wp_get_current_user();
  if ( empty( $user ) )
    return false;
  return in_array( $role, (array) $user->roles );
}

Add the Custom Title Field

You will also need to add this to your functions.php file. The Title Field function included is borrowed from dkukral’s User Title plugin.

if ( check_user_role( 'author' ) ) {

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

  function user_title_field ($user) { ?>
<h3><?php _e("User Title", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="title"><?php _e("Title"); ?></label></th>
<td>
<input type="text" name="title" id="title" value="<?php echo esc_attr(get_user_meta($user->id, "user_title", True)); ?>" class="regular-text"/><br/>
<span class="description"><?php _e("Please enter user title"); ?></span>
</td>
</tr>
</table>
<?php }

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

  function save_user_title_field($user_id) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
    update_user_meta($user_id, 'user_title', $_POST['title']);
  }

}

Summary

As you can imagine, you could go a lot of places with this concept. The function could be used in other ways too. For me, it solved a big piece of the puzzle as far as editing profiles go.

Published by

Tim Bunch

Tim Bunch is a Web Designer and Social Media Specialist at CapEd FCU from Boise, Idaho. As a web standards fanatic, he passionately pursues best practices in web design. He also actively engages people on a wide range of topics in a variety of social media networks. Tim is also an avid Wordpress developer, music maker, coffee drinker, and child raiser. @timbunch

Leave a Reply

Your email address will not be published. Required fields are marked *