Grouping users under parent user

Roles

First of all you need to register the 2 roles, look at add_role.

When you register the role, you are free to assign any capability you want.

Only be careful to add the roles when your theme / plugin is activated and possibly remove them (see remove_role) when it is disabled.


Meta Data

You can do the association using a meta entry like company_employees that stores an array of user IDs. You can associate some custom function to get / set employees to a company user.

Get Employees of a Company

Example code for a function that return an array of employees given an user:

function getEmployees(\WP_User $user) {
  // if the user has not the role "company_role" can't have employees
  if ( ! in_array('company_role', $user->roles, true) ) {
    return array();
  }
  $meta = get_user_meta($user->ID, 'company_employees', true);
  if (empty($meta)) {
    return array();
  }

  $query = new WP_User_Query(array(
    'role'    => 'employee_role',
    'include' => (array) $meta
  ));

  return $query->results;
}

Assign Employee to a Company

In the same way you can write a function to assign an employee to a company user:

function assignToCompany(\WP_User $employee, \WP_User $company) {
  // if the company user has not the role "company_role" can't have employees
  if ( ! in_array('company_role', $company->roles, true) ) {
     return false;
  }
  // if the employee user has not the role "employee_role" can't be assigned
  if ( ! in_array('employee_role', $employee->roles, true) ) {
     return false;
  }
  // get current employees
  $employees = get_user_meta($company->ID, 'company_employees', true);
  is_empty($employees) and $employees = array();
  // add employee and update
  $employees[] = $employee->ID;
  $update = update_user_meta($company->ID, 'company_employees', $employees);

  return (int) $update > 0;
}

UI

Of course, you need some sort of UI to assign employees to users. If an employee may have only one company, you can show a dropdown menu in the user edit page where you list all the companies and, on save, you can leverage the function above to store the employee in the company user meta.

To do that:

  • look at edit_user_profile hook to print the dropdown menu. Be sure to show the menu only to users that are allowed to edit the employee company
  • look at edit_user_profile_update hook to save the company in the meta field. Be sure to proceed with save only if current user is allowed to edit the employee company
  • be sure to also add a nonce field when you print the dropdown menu, and verify it before proceed with save. See wp_nonce_field() and wp_verify_nonce()

Final Touches

Company User Admin UI

You can add the list of the employees currently assigned to a company user in the user edit page.

Make it editable is a bonus.

Utility functions

You can add a couple of utility functions like isEmployee(), isCompany() that accept an user object ot id and return a true / false if the user has the related role.

isEmployee might also accept an optional param with a company user, and return true only if the user is an employee of that specific company.

Custom Capability

It may be a good idea to introduce a custom capability 'assign_employee' that you can assign to administrators, and to any other role you want to be able to assign employees users to company users. This could be useful when you need to show rthe company dropdown and when you have to save the company meta.

With this capability in place you can check if an user is allowed to do that using current_user_can('assign_employee').

To assign a custom capability to an existent role, have a look to WP_Roles::add_cap().

If you decide to do this, remember to remove the capability when the plugin / theme is disabled.

Frontend Company Page

Don’t know if you need it, but you can create a “company page” in frontend, using a custom page template and a custom endpoint to show the user company description and a list of all employees

Leave a Comment