Home › Forums › Feature Requests › Feature Request: Hidden / Disabled Field Type
10 out 10 for the plugin!
Just wanted to say that if you could add this feature to your plugin you would solve a lot of peoples needs from all the google searching I have done on the subject that follows suit of what you have created.
There has been times I need to display the fields values but not let it be edited by certain user roles for front end forms and added profile page fields so if the following could be added to your plugin it would be a massive help.
My work around for front end forms was to created if(user roles can) and then creating the forms for each role to display only editiable fields and echo out the data at top from fields that was removed.
Feature request would be:
Tick box for selecting if the content data should be displayed and if this is selected add a before and after field for allowing html values so the user can style the displayed data.
readonly and hidden fields are not in the scope of what I built my plugin for. I also feel that the settings for such a thing would become unwieldy. Imagine checkboxes for every user role (edit, readonly, disabled), or even a radio field. I think this would over-complicate the setting and makes it less useful.
Hidden fields are extremely difficult in ACF because there isn’t a way to provide hidden fields, and as I stated in the plugin, there is no security in hidden fields and I find them a useless endeavor. The only purpose that a hidden field serves would be if the value is needed for some type of javascript that you want to perform and if this is the case then this purpose would be better served by localizing your script with the values you’ll need.
To turn this back to the subject of ACF itself rather than my plugin let’s talk about displaying a field so it can be read but not edited…
Read only fields, as you are talking about, would have to be done with a completely different mechanism. I think this could be done by using an acf/load_field filter and converting the field from whatever type of field it is into a message field, taking the values of the field and converting them to a message. I’m not even sure if this is possible or if it would somehow effect the values saved by ACF. But then this could also be accomplished by having a standard ACF message field that is conditional on the user type and then using an acf/load_field filter to set the message in a similar fashion by getting the message from the value of the field that you want to display.
I just needed it. No need to complicate. Or add unnecessary extra plugin and code.
https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/
First snippet adds Admin only view (new option) for fields, second snippet remove it even from Admins. Do not forget to activate Admin only check option for second snippet to work.
add_filter( 'acf/load_field/name=inscription_classe', [ $this, 'hide_field' ] );
add_action('acf/render_field_settings', 'my_admin_only_render_field_settings');
function my_admin_only_render_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __('Admin Only?'),
'instructions' => '',
'name' => 'admin_only',
'type' => 'true_false',
'ui' => 1,
), true);
}
add_filter('acf/prepare_field', 'my_admin_only_prepare_field');
function my_admin_only_prepare_field( $field ) {
// bail early if no 'admin_only' setting
if( empty($field['admin_only']) ) return $field;
// return false if is not admin (removes field)
if( !current_user_can('administrator') ) return false;
// return
// return $field;
}
That is basically what my plugin does, except that it includes all user types and all field types.
If you’re only doing this for one field and one user type I don’t even think you need the setting.
add_filter('acf/prepare_field/name=inscription_classe', 'my_admin_only_prepare_field');
function my_admin_only_prepare_field( $field ) {
// return false if is not admin (removes field)
if( !current_user_can('administrator') ) return false;
return $field;
}
The topic ‘Feature Request: Hidden / Disabled Field Type’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.