0

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(
    'list_users',
    'edit_users',
    'create_users',
    'delete_users',
  );
  $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?

2
  • 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
    Commented May 22, 2015 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 Commented May 22, 2015 at 5:42

2 Answers 2

3

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){
    if(is_object($user))
        $created_by = esc_attr( get_the_author_meta( 'created_by', $user->ID ) );
    else
        $created_by = null;
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="created_by">Created By</label></th>
            <td>
                <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>
            </td>
        </tr>
    </table>
<?php
}
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
    //if(!current_user_can('manage_options'))
      //  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..

2
  • 1
    you put this in custom_functions.php or fuctions.php right? thanks for this!
    – Brock Vond
    Commented Aug 30, 2015 at 8:53
  • @BrockVond Yes. I put this in my functions.php Commented Sep 1, 2015 at 12:04
0

I used the plugin Snippets to put this and other custom functions into the site. Keeps the functions.php file clean.

1
  • You can also make a simple plugin yourself. I'm always a bit nervous about giving site admins a way to run arbitrary PHP code from the dashboard.
    – Rup
    Commented Feb 2, 2023 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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