Home › Forums › Backend Issues (wp-admin) › Admin only custom field setting outputs empty input fields
Hi,
I wanted to include the ‘Admin Only’ custom field setting as described in the first example here: https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/
It doesn’t display the fields as expected but instead it shows empty input fields all having the same wrappers/classes (see attached images).
I have the ‘Admin Only’ setting applied to fields that are included in a Layout field block via the clone field (prefixed, no group).
Any idea where these input fields are coming from and how to get rid of them or at least hide them?
Thanks!
Lorenz
sure, but it’s identical to the one on https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/
function acf_admin_only_field_setting( $field ) {
acf_render_field_setting( $field, array(
'label' => __('Admin Only?'),
'instructions' => '',
'name' => 'admin_only',
'type' => 'true_false',
'ui' => 1,
), true);
}
add_action('acf/render_field_settings', 'acf_admin_only_field_setting');
function acf_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;
}
add_filter('acf/prepare_field', 'acf_admin_only_prepare_field');
Hmm, you’re right. But if you’re using load_field hook instead of prepare_field, it works:
add_filter('acf/load_field', 'acf_admin_only_prepare_field');
That causes multiple undefined index errors (see attachment) and even the message field at the beginning isn’t visible anymore…
Hi @lorenz
I believe the error messages were showing up on the field group editor page, right? To fix it, could you please try the following code instead?
add_filter('acf/load_field', 'my_admin_only_prepare_field', 99);
function my_admin_only_prepare_field( $field ) {
// Don't do this on the field group editor
$screen = get_current_screen();
if( $screen->post_type == 'acf-field-group' ) return $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 false;
//return $field;
}
I hope this helps 🙂
Hi James
Thank you for your reply. Unfortunately the error is not on the field group editor page. It occurs within the editor view of a page where the fields should (or better: should not) be displayed. Sorry if that was not clear enough before.
I try to describe the situation again (hopefully a bit clearer than before):
The fields with the admin_only option are placed inside a layout field item using a single clone field. This layout field item (among other similar layout field items) are used on pages that use a specific page-template.
Usually, the documentation on the site is a little behind. In this case the documentation is actually ahead of the current version. This is something that has been added for 5.5, which hasn’t been released yet.
The first hint that this was the case was new “slide” on/off switches for the true false fields, which is another feature that’s in 5.5. I had a look into the code and confirmed that ACF checks the value returned from the filters.
In the current version there isn’t any check to see if the value that’s returned from your filter is false or is a field, and it still tries to display the field, which causes PHP errors.
The topic ‘Admin only custom field setting outputs empty input fields’ 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.