To hide custom fields for non-admin users in the WordPress admin area, you can use the admin_head action hook and CSS to hide the Custom Fields metabox. Add the following code snippet to your theme’s functions.php file:
- In your WordPress admin dashboard, go to “Appearance” > “Theme Editor.”
- In the right sidebar, locate and click on the “functions.php” file.
- Add the following code snippet at the end of the file:
function hide_custom_fields_for_non_admins() {
// Check if the current user is not an admin
if (!current_user_can('manage_options')) {
echo '<style>
/* Hide the Custom Fields metabox */
#postcustom { display: none !important; }
</style>';
}
}
add_action('admin_head', 'hide_custom_fields_for_non_admins');
This code snippet creates a function called hide_custom_fields_for_non_admins() that checks if the current user is not an admin. If the user is not an admin, it adds CSS to hide the Custom Fields metabox in the admin area.
- Click “Update File” to save the changes.
Now, the Custom Fields metabox will be hidden for non-admin users in the WordPress admin area.
Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.