Administration

The admin utility function provides a tool for customizing the WordPress admin interface by disabling specific fields.

Available Function

cbo_admin_disable_field($fields)

Disables specific fields in the WordPress admin interface and displays a notification explaining why they are disabled.

Parameters:

  • $fields: Array of field names to disable.
1
// Disable specific ACF fields
2
cbo_admin_disable_field([
3
'field_5f8a9b7c12345',
4
'field_5f8a9b7c67890'
5
]);
6
7
// This will:
8
// 1. Enqueue the admin-disabled-fields.js script
9
// 2. Pass the field names to JavaScript
10
// 3. Display an admin notice explaining that some settings are disabled

How It Works

The function works by:

  1. Enqueuing a JavaScript file that handles the field disabling
  2. Passing the list of fields to disable to the JavaScript via wp_localize_script
  3. Adding an admin notice to inform users why certain fields are disabled

The JavaScript file finds the specified fields in the admin interface and disables them, preventing users from modifying values that are defined in the Combo configuration files.

Usage Examples

Disabling Theme Option Fields

1
// Disable theme option fields that are managed by configuration
2
function disable_configured_theme_options() {
3
// Only run on the options page
4
$screen = get_current_screen();
5
if ($screen->id !== 'appearance_page_theme-options') {
6
return;
7
}
8
9
// Fields to disable (ACF field keys)
10
$disabled_fields = [
11
'field_5f8a9b7c12345', // Logo
12
'field_5f8a9b7c67890', // Primary Color
13
'field_5f8a9b7c54321' // Footer Text
14
];
15
16
// Disable the fields
17
cbo_admin_disable_field($disabled_fields);
18
}
19
add_action('admin_init', 'disable_configured_theme_options');

Disabling Post Type Fields

1
// Disable specific fields for a custom post type
2
function disable_product_fields() {
3
// Only run on the product edit screen
4
$screen = get_current_screen();
5
if ($screen->id !== 'product' || $screen->base !== 'post') {
6
return;
7
}
8
9
// Fields to disable (ACF field keys)
10
$disabled_fields = [
11
'field_5f8a9b7c11111', // Price
12
'field_5f8a9b7c22222', // SKU
13
'field_5f8a9b7c33333' // Stock Status
14
];
15
16
// Disable the fields
17
cbo_admin_disable_field($disabled_fields);
18
}
19
add_action('admin_init', 'disable_product_fields');

Best Practices

  • Use sparingly to avoid frustrating users who expect to be able to edit fields.
  • Provide clear documentation explaining which fields are disabled and why.
  • Consider user roles when deciding which fields to disable (e.g., allow administrators to edit fields that editors cannot).
  • Test thoroughly to ensure the correct fields are being disabled.
  • Use in combination with configuration files to provide a consistent experience.