Make specific Advanced Custom Field read-only

To make a specific advanced custom field read-only from the backend in WordPress, you need to identify the field’s name and add a conditional statement to the function.

Here is an example of how you can modify the previous function to make a field with a specific name read-only:


/**
* Make specific Advanced Custom Field read-only in the backend.
*
* @param array $field
* @return array
*/
function make_field_readonly($field) {
// Change 'field_name' to the name of the field you want to make read-only
if($field['name'] === 'field_name') {
$field['readonly'] = 1;
}
return $field;
}
add_filter('acf/load_field', 'make_field_readonly');

In this example, the function uses an if statement to check if the field’s name matches the specified name, which is ‘field_name‘ in this case. If the field name matches, the function sets the readonly attribute to 1, making the field read-only in the backend.

Remember to replace ‘field_name‘ with the name of your specific field.