Add ‘Creator’ User Meta when adding user

I am creating a custom web using WordPress. I need to get information about who has created an account/user.

The meta for example created_by. So that I can retrieve who are the users that created by user X and who are the user created by Y and so on.

Related posts

1 comment

  1. //Whenever a user will be created 'user_register' hook of WordPress is executed
    
    add_action( 'user_register', 'meta_registration_save', 10, 1 );
    
    //Following function will be called on each user registration and will save the 'created_by' information in wp_usermeta table
    function meta_registration_save( $user_id ) {
    
        //logged in user
        if ( is_user_logged_in() )
        {
            global $current_user;
            get_currentuserinfo();
            $current_id=$current_user->ID;
            add_user_meta( $user_id, 'Created_by', $id );
        }
        else{
            //in case user is registered from non logged in user
            add_user_meta( $user_id, 'Created_by', 0 ); 
        }
    }
    
    //To get the user meta details call following function
    
    function get_creator_of_user($userid)
    {
        if(( $creator_id=get_user_meta ($userid, 'Created_by',true) )!= 0)
            echo get_user_meta ($creator_id, 'nickname',true);
    
         else
            echo 'User created by non logged in user';
    }
    

Comments are closed.