I have two users with the capability to create other users. But their roles are not same.

  1. Tom - Role is Administrator
  2. Jackie - Role is CMS Manger (a custom created role)

Both have capability to create new users. CMS Manger gets this capability by following code:

add_action( 'after_setup_theme', 'add_caps_to_custom_roles' );
function add_caps_to_custom_roles() {
  $caps = array(
  $roles = array(
    get_role( 'CMS Manager' ),
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );

I have 120 users in the site now. But do not know who created whom. I want a list that shows :

  • List of users created by tom- Tom's users
  • List of users created by jackie- Jackie's users

I was referring this for a while : https://codex.wordpress.org/Function_Reference/get_user_by But seems that function has NO solution for the above type of list.

How can I create those lists?

  • Who created a user is not stored anywhere, you will need to store that information at your own. You could use user meta fields and then use WP_User_Query to get the users by custom fields parameters. – cybmeta May 22 '15 at 5:22
  • yup.. I can do that after the user registration using the hook user_contactmethods. Is there any way to add a custom field during a NEW user registration? Now I am looking for a solution by using the hook user_new_formwhicj introduced in WP 3.7 – Riffaz Starr May 22 '15 at 5:42

I've found it.

/*** Adding extra field to get the the user who creates the another user during ADD NEW USER ***/
function custom_user_profile_fields($user){
        $created_by = esc_attr( get_the_author_meta( 'created_by', $user->ID ) );
        $created_by = null;
    <h3>Extra profile information</h3>
    <table class="form-table">
            <th><label for="created_by">Created By</label></th>
                <input type="text" class="regular-text" name="created_by" value="<?php echo $created_by; ?>" id="created_by" /><br />
                <span class="description">The person who creates this user</span>
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
      //  return false;

    # save my custom field
    update_user_meta($user_id, 'created_by', $_POST['created_by']);
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');

And then querying the users by the custom field created_by.. That's it..

  • 1
    you put this in custom_functions.php or fuctions.php right? thanks for this! – Brock Vond Aug 30 '15 at 8:53
  • @BrockVond Yes. I put this in my functions.php – Riffaz Starr Sep 1 '15 at 12:04

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.