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 fields2cbo_admin_disable_field([3'field_5f8a9b7c12345',4'field_5f8a9b7c67890'5]);67// This will:8// 1. Enqueue the admin-disabled-fields.js script9// 2. Pass the field names to JavaScript10// 3. Display an admin notice explaining that some settings are disabled
How It Works
The function works by:
- Enqueuing a JavaScript file that handles the field disabling
- Passing the list of fields to disable to the JavaScript via wp_localize_script
- 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 configuration2function disable_configured_theme_options() {3// Only run on the options page4$screen = get_current_screen();5if ($screen->id !== 'appearance_page_theme-options') {6return;7}89// Fields to disable (ACF field keys)10$disabled_fields = [11'field_5f8a9b7c12345', // Logo12'field_5f8a9b7c67890', // Primary Color13'field_5f8a9b7c54321' // Footer Text14];1516// Disable the fields17cbo_admin_disable_field($disabled_fields);18}19add_action('admin_init', 'disable_configured_theme_options');
Disabling Post Type Fields
1// Disable specific fields for a custom post type2function disable_product_fields() {3// Only run on the product edit screen4$screen = get_current_screen();5if ($screen->id !== 'product' || $screen->base !== 'post') {6return;7}89// Fields to disable (ACF field keys)10$disabled_fields = [11'field_5f8a9b7c11111', // Price12'field_5f8a9b7c22222', // SKU13'field_5f8a9b7c33333' // Stock Status14];1516// Disable the fields17cbo_admin_disable_field($disabled_fields);18}19add_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.