Home Forums General Issues Editing an ACF field and submitting in $_POST


Editing an ACF field and submitting in $_POST

  • I want to display an input for an acf field on a custom user profile edit page so I can include it in the forms $_POST data when it is submitted. I can display in it’s raw text form and save successfully, but I want to be able for the user to change using the ACF input method… i.e. for a date – using the datepicker, or image, or range, or checkbox … etc

    I’m not using an acf_form as I have mixed contend from a number of different sources and want to save all with a single submit button.

    I’ve only just started using ACF, so this is probably a real newby question, but I can’t see anything in the documentation, but it must be really easy … surely?

    I have something like this …

    // Update acf_date_of_birth
    <?php if ( !empty( $_POST['acf_date_of_birth'] ) ) {
      update_field('acf_date_of_birth', esc_attr( $_POST['acf_date_of_birth'] ), $acfpostid );
    } ?>
    <!-- input acf_date_of_birth -->
    <input class="hasDatepicker" name="acf_date_of_birth" type="text" id="acf_date_of_birth" value="<?php the_field( 'acf_date_of_birth', $acfpostid ); ?>" />

    I’m currently using the free plugin: v4.4.12
    and WordPress v4.9.8

  • You should still be able to use acf_form() ( similar to how you would a front-end from ) tell it not to display the form just the fields:

    Even though it’s not on the front-end it’s still a way to get a Field Group into a custom form:

    acf_form( array(
    	'form' 			=> false,
    	'field_groups' 	=> array( 'acfieldgroupkeyhere' ),
    ) );

    You would still need to call acf_form_head() to get the neat functionality and styles ACF provides. The biggest problem with this is that the fields are POSTed into an array $_POST[‘acf’] and keyed by the Field IDs instead of the field names – you can either either keep track of the field IDs in your code and what ID gets saved where or you can loop through the ACF fields array by Key to get the name using get_field_object( $key ).

  • Thanks for your reply – that’s been really helpful and I’ve pretty much got it working as I need to now. It was a nice surprise to find that the acf field data seems to be saved automatically without having to program manually. However, I don’t seem to be getting anything POSTed in the $_POST(‘acf’) array. I wondered if it was because I was running the free version (v4)? I have now upgraded to Pro (v5) and it still doesn’t seem to bring anything through. Is there an additional setting in acf_form() options that I have to set?

  • As far as I know it should be the same throughout all versions. You could turn on error log debugging and use the acf/save_post hook to error log your $_POSTed values and see where your fields are:

     * Error log processing ACF form saves
     * @param Mixed $post_id
     * @return void
    function prefix_acf_save_post( $post_id ) {
    	// Don't run on the admin panel, front-end forms only
    	if( is_admin() ) {
    	error_log( sprintf( 'Front-end Save Post: %1$s', $post_id ) );
    	error_log( print_r( $_POST, 1 ) );
    add_action( 'acf/save_post', 'prefix_acf_save_post', 9 );
  • Thanks again for your help.

    I placed the code above into my functions.php file, but the event doesn’t seem to be firing. Neither am I getting anything coming through in the $_POST(‘acf’)

    Turns out it is only saving my acf fields data, and is no longer saving my other meta data now. When I click the ‘Submit’ button, it now seems to coming back as a GET request method????? Maybe the acf_form_head() is redirecting again once it has save the ACF fields it knows about, so it’s never getting to my POST processing code?

    I need to do some more testing I think… will post an update once I have figured out whats going on.

  • This may be a silly question but are you sure your <form> tag has method="post" set? I was under the assumption you have a custom <form> wrapper to include you custom inputs along with an acf_form() with a field group of inputs. It may be easier to replicate and debug given the full form code.

    If you figure out the issue though be sure to come back and let us know!

  • Haha … yes definitely have a custom <form> with a method="post" !
    It was working fine before I added the acf_form() and the acf_form_head()!
    I was expecting to have to save the ‘acf’ data as you described above.

    I’ll cobble together a concise full example and post here if I’m still running into issues.
    Thanks again

  • The issue is that ACF is running to save the values in the ACF fields and then it is redirecting, probably before your other code to save other fields is running. What you need to do to create a filter/action for ACF and then run your other code for saving the other data in this action filter instead of where/when you are saving it. More than likely you could use an acf/save_post action and since it’s a front end form you could probably also use an acf/pre_save_post filter, but I’d probably go with the first one in this case.

  • Adding my validation and non-acf fields update code into the ‘acf/save_post’ (pre) has resolved my issue. Thank you gentlemen for your help!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Editing an ACF field and submitting in $_POST’ is closed to new replies.