WP Sheet Editor provides a powerful spreadsheet interface for managing your WordPress content. For developers and power users, there might be a need to control which features are available to different users. This tutorial provides a straightforward PHP code snippet to programmatically remove toolbar items, effectively creating a read-only or restricted-feature experience for non-administrative users.
IMPORTANT: This is just a user interface/aesthetic change, it’s not a security feature. The user still has the WordPress capabilities to edit in the regular screens and in other places. Removing the tools related to modifying data does not remove their capabilities or permissions from other places in WordPress.
The Code Snippet
Here is the complete code snippet that removes the Save, Bulk Edit, and Import tools for any user who does not have the ‘manage_options’ capability (typically administrators).
<?php
/**
* WP Sheet Editor - Restrict Toolbar Items
*
* This code snippet restricts access to data modification tools in WP Sheet Editor
* for all users except administrators.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'vg_sheet_editor/initialized', 'wpse_tools_per_role_after_core_init' );
/**
* Initializes the filters after WP Sheet Editor is loaded.
*/
function wpse_tools_per_role_after_core_init() {
if ( ! class_exists( 'WP_Sheet_Editor' ) ) {
return;
}
add_filter( 'vg_sheet_editor/toolbar/get_items', 'wpse_filter_toolbar_items', 99 );
add_filter( 'vg_sheet_editor/js_data', 'wpse_make_sheet_readonly', 10, 2 );
}
/**
* Filters the toolbar items to remove data modification tools.
*
* @param array $items The registered toolbar items.
* @return array The filtered toolbar items.
*/
function wpse_filter_toolbar_items( $items ) {
// Do not remove tools for administrators.
if ( current_user_can( 'manage_options' ) ) {
return $items;
}
$keys_to_remove = array( 'save', 'run_formulas', 'import_csv' );
foreach ( $items as $provider => &$toolbars ) {
foreach ( $toolbars as $toolbar_key => &$toolbar_items ) {
foreach ( $keys_to_remove as $key_to_remove ) {
if ( isset( $toolbar_items[ $key_to_remove ] ) ) {
unset( $toolbar_items[ $key_to_remove ] );
if ( 'save' === $key_to_remove ) {
$GLOBALS['wpse_save_button_removed'] = true;
}
}
}
}
}
return $items;
}
/**
* Makes the spreadsheet read-only if the 'save' button is removed.
*
* @param array $data The JavaScript data for the editor.
* @param string $post_type The current post type being edited.
* @return array The modified JavaScript data.
*/
function wpse_make_sheet_readonly( $data, $post_type ) {
if ( ! empty( $GLOBALS['wpse_save_button_removed'] ) ) {
if ( ! isset( $data['custom_handsontable_args'] ) ) {
$data['custom_handsontable_args'] = array();
}
$data['custom_handsontable_args']['readOnly'] = true;
}
return $data;
}
?>
How to Implement the Code
You can add this PHP code snippet to your website in one of two ways:
- Add it to the functions.php file of your active child theme.
- Create a custom plugin specifically for this functionality.
- Or use a code snippets plugin
How It Works
The snippet uses two primary functions hooked into WP Sheet Editor’s actions and filters:
- wpse_filter_toolbar_items: This function is attached to the
vg_sheet_editor/toolbar/get_items
filter. It first checks if the current user is an administrator. If not, it iterates through all the toolbars and removes items whose keys are listed in the$keys_to_remove
array. It also sets a global flag if the ‘save’ button is removed. - wpse_make_sheet_readonly: This function hooks into
vg_sheet_editor/js_data
. It checks the global flag set by the previous function. If the ‘save’ button was removed, it injects a setting into the spreadsheet’s configuration to make it entirely read-only, preventing users from making edits they cannot save.
How to Customize the Code
This snippet can be easily adapted to fit more specific requirements.
Removing Different Toolbar Items
To remove different tools, you need to modify the
$keys_to_remove
array inside the
wpse_filter_toolbar_items
function. Here is a comprehensive list of toolbar keys available, which you can use to customize the user experience.
Primary Toolbar (Top)
-
save
: Save changes -
add_rows
: Add new row(s) -
load
/run_filters
: Load rows or apply search filters -
run_formula
: The “Bulk Edit” tool (also includes individual formula editing) -
import_csv
: Import tool -
export_csv
: Export tool -
duplicate
: Duplicate rows -
exit_full_screen
: Exit full screen mode -
ai_add_new
: AI – Add new rows -
ai_add_new_shortcut
: AI – Add new rows (shortcut) -
saved_searches
: Saved searches -
quick_bulk_edits1
toquick_bulk_edits7
: Quick bulk edit buttons -
ai
: General AI tools -
cell_locator
: Go to a specific cell -
column_locator
: Go to a specific column -
scheduled_ai_add_new
: Scheduled AI row creation -
scheduled_bulk_edits
: Scheduled bulk edits -
save_rows_later
: Save rows later -
scheduled_save_rows
: Scheduled row saving
Secondary Toolbar
-
settings
/advanced_settings
: Settings -
support
: Support resources -
extensions
: View extensions -
default_sort
: Default sort options -
cells_format
: Cell formatting options -
infinite_scroll
: Toggle infinite scroll -
toggle_dark_mode
: Toggle dark mode -
rescan_db
: Rescan database for new fields -
columns_manager
/columns_visibility
: Columns Manager -
add_columns
: Add custom columns -
save_export0
tosave_export11
: Saved export profiles -
run_previous_import
: Run a previous import -
ai_completions_log
,ai_providers
,ai_prompts
,ai_chat
: AI-related tools -
wpse_cron_setup
,error_log
,troubleshooting
: Developer and debugging tools -
google_sheets
,google_sheets_create_new
,google_sheets_quick_setup
: Google Sheets integration -
scheduled_exports
,scheduled_imports
: Scheduled jobs -
wpse_license
My license
Example: To also remove the “Export” tools, modify the array like this:
$keys_to_remove = array( 'save', 'run_formula', 'import_csv', 'export_csv' );
Restricting Tools on a Specific Spreadsheet
The
wpse_filter_toolbar_items
function iterates through all registered spreadsheets using the
$provider
variable, which holds the sheet key (e.g., ‘post’, ‘product’, ‘user’). You can add a conditional check to apply restrictions only to a specific sheet.
Example: To apply these restrictions only to the WooCommerce Products spreadsheet:
function wpse_filter_toolbar_items( $items ) {
if ( current_user_can( 'manage_options' ) ) {
return $items;
}
$keys_to_remove = array( 'save', 'run_formula', 'import_csv' );
foreach ( $items as $provider => &$toolbars ) {
// Only apply to the 'product' sheet
if ( $provider === 'product' ) {
foreach ( $toolbars as $toolbar_key => &$toolbar_items ) {
foreach ( $keys_to_remove as $key_to_remove ) {
if ( isset( $toolbar_items[ $key_to_remove ] ) ) {
unset( $toolbar_items[ $key_to_remove ] );
if ( 'save' === $key_to_remove ) {
$GLOBALS['wpse_save_button_removed'] = true;
}
}
}
}
}
}
return $items;
}
Restricting Tools for Specific User Roles
Instead of checking for the ‘manage_options’ capability, you can implement more granular logic based on specific user roles.
Example: To hide the tools from users with the ‘editor’ role:
function wpse_filter_toolbar_items( $items ) {
$user = wp_get_current_user();
$user_roles = (array) $user->roles;
// Remove tools if the user is an 'editor'
if ( in_array( 'editor', $user_roles, true ) ) {
$keys_to_remove = array( 'save', 'run_formula', 'import_csv' );
foreach ( $items as $provider => &$toolbars ) {
// ... (rest of the removal logic)
}
}
return $items;
}