How to edit multiple user roles in the spreadsheet

| 0

When you use the “users spreadsheet”, you see a column for editing the user role. By default WordPress assigns one role to each user. But some websites use a plugin to assign multiple roles to every user.

In this case we’ll show you how to edit multiple roles per user on the spreadsheet.

This is based on the “multiple roles” plugin.

Get the plugin

You can download this as WordPress plugin. You just need to install the zip in your site and it will work automatically.

Note. You need the “wp sheet editor – users” spreadsheet active, which is a premium plugin that provides the users spreadsheet. This free plugin is an extension of the users spreadsheet.

If you’re a developer you can read this post to understand what the plugin does and how it’s coded.

1- Register the new column on the spreadsheet

This snippet is very simple. We use the before_init hook to modify the spreadsheet object.

We make the modification only if the spreadsheet provider = ‘user’ (users spreadsheet).

We register a new column passing the column key (wpse_multiple_roles), spreadsheet key (user), and column settings.

add_action('vg_sheet_editor/editor/before_init', 'register_columns');
function register_columns($editor) {
  if ($editor->args['provider'] === 'user') {
    $editor->args['columns']->register_item('wpse_multiple_roles, 'user', array(
      'data_type' => 'meta_data',
      'supports_formulas' => true,
    ));
  }
}

This will add a text cell to the spreadsheet and we can edit and save the cell.

2- Modify the data before saving the cell

By default the spreadsheet saves the cell as plain text. In this case, the “multiple roles” plugin requires the roles as array. So we need to modify the value before it’s saved to the database.

add_filter('vg_sheet_editor/provider/user/update_item_meta', 'filter_cell_data_for_saving', 10, 3);
function filter_cell_data_for_saving($new_value, $id, $key) {
  global $wpdb;
  if ($key === 'wpse_multiple_roles') {
    $roles = array_fill_keys(array_values(array_map('trim', explode(',', $new_value))), 1);
    // We can't save empty roles, the user must have one role at least
    if (!empty($roles)) {
      update_user_meta($id, $wpdb->prefix . 'capabilities', $roles);
    }
    $new_value = '';
  }

  return $new_value;
}

The filter “vg_sheet_editor/provider/user/update_item_meta” runs before saving every meta field. Inside we check if the field key equals the “multiple roles” column, we convert the string from “role1, role2, role3” to an array like this, and save the field directly. We return an empty string, so the spreadsheet saves the fake “multiple roles” field as empty because we’ll never use it:

array(
"editor" => 1,
"author" => 1,
)

3- Modify the data for reading

Now we need to do the opposite. We get the data from the database as array and we need to convert it to a string with roles separated by commas.

add_filter('vg_sheet_editor/provider/user/get_item_meta', 'filter_cell_data_for_readings', 10, 5);
function filter_cell_data_for_readings($value, $id, $key, $single, $context) {
  global $wpdb;
  if ($context !== 'read') {
    return $value;
  }

  if ($key === 'wpse_multiple_roles') {
    $roles = get_user_meta($id, $wpdb->prefix . 'capabilities', true);
    $value = ( is_array($roles) ) ? implode(', ', array_keys($roles)) : '';
  }

  return $value;
}

We’re doing something similar to the previous step. We use a filter to modify the value, we bail if the context is not “read” (this filter is for reading data, but we might be reading data on a saving context), we fetch the raw data from the database, convert it to string, and return the string.