I’m using acf_form to make a user profile editable. I’m storing the information in various groups containing multiple fields, when I simple use a field_groups without specifying the fields the form is working fine but when I try to specify fields with their keys, the form is visible but there’s no data with the fields. I get empty input fields.
Here’s my code:
acf_form_head();
$acf_args12 = array(
'id' => 'acf-form-left',
'post_id' => 25869,
'new_post' => false,
'field_groups' => array('field_5fc11480d06ea'),
'fields' => array('field_5fc265e096135', 'field_5fc65682c81ce'),
'return' => '',
'uploader' => 'basic',
'html_before_fields' =>'<div class="container"><div class="left">',
'html_after_fields' => '</div></div>',
);
acf_form($acf_args12);
My requirement is I need only some of the fields of a certain group.
Need more information on the fields you’re trying to use.
Are these fields sub fields of other fields? If this is the case you cannot specify just the sub fields.
Yes the fields I’m trying to get are subfields of a group field.
An example would be:
Group Field: Admin
Sub Field:
Label: Years in practice Type: Text
You cannot edit only the sub fields on a front end form without including the parent field.
The way to do this would be to include the group field in your front end form and then use an acf/prepare_field filter for each of the fields that you do not want to show and return false for these fields on the front end. Use the field key variant.
a quick example
add_filter('acf/prepare_field/key=field_XXXXXXX', 'hide_acf_field_on_front');
function hide_acf_field_on_front($field) {
if (is_admin()) {
// allow field in admin
return $field;
}
// hide on front
return false;
}
I understand what you’ve mentioned. This works but I’ve run into a bit of a mess here, is there any way that I can have multiple forms for the same group field with different forms containing different sub fields on multiple locations on the same page.
I hope I’m clear enough with my description.
It is possible to do this. When calling acf_form() for each part you would set the form argument to false "form' => false
. This prevents ACF from adding the form tags and the submit button. You would then supply your own <form></form>
tags and your own submit button that wraps all of the other fields that are output making them work as a single form.
One final query, is there any way that I can hide same sub field in one section and show it in the other on the same page with multiple acf_form.
That is going to be more difficult. The only way I can think of, and I hate to say it, is to set and check a global variable.
// set global
global $my_current_form;
// set form to the current form being shown
$my_current_form = 'form 1';
// call acf form
acf_form(....
// add to filter
add_filter('acf/prepare_field/key=field_XXXXXXX', 'hide_acf_field_on_front');
function hide_acf_field_on_front($field) {
global $my_current_form ;
if (is_admin() || $my_current_form == 'form 1')) {
// allow field in admin
// allow the field on "form 1"
return $field;
}
// remove field
return false;
}
You must be logged in to reply to this topic.
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.