0

I have made a front-end form that allows the users to set\update a few user meta fields made with Advanced Custom Fields (there are user preferences, mostly: dark\light mode, etc.).

The preferences are set through an HTML form:

        <form id="cl_style_form" class="cl_style_form" action="" method="post">
            <input id="cl_dark_input" name="cl_dark_input" type="hidden" value="<?php echo $cl_dark; ?>"/>
            <input id="cl_compact_input" name="cl_compact_input" type="hidden" value="<?php echo $cl_compact; ?>"/>
            <input id="cl_style_form_submit" name="cl_style_form_submit" type="submit"/>
        </form>

and sent using the PHP Post method:

        if(isset($_POST['cl_style_form_submit'])){
            update_field('cl_dark', $_POST['cl_dark_input'], 'user_'.$cl_user);
            update_field('cl_compact', $_POST['cl_compact_input'], 'user_'.$cl_user);
        }

I am, however, encountering an issue. Whenever the submit button is pressed and the page is reloaded, the changes are not applied. Reloading the page a second time (without re-submitting the form data) applied the changes correctly.

I am not sure why this is happening.

Maybe the reload happens when the fields are still being updated?

  • Are you saving the data prior to displaying them? And in what hook you call the update_field()? – Sally CJ Aug 3 '19 at 0:52
  • Hi! As far as I am aware the data is saved by the update_field function itself - no further action is required (the data is correctly updated in the backend). The update field is called whenever the user clicks on the submit button. The only issue is that the changes do not seem to be applied on the first refresh of the page, but rather on the second. – Campioni Del Mondo Aug 3 '19 at 10:23
  • Hi. I'm actually saying that you may have called the update_field() after your form is rendered? Could you show your full code? – Sally CJ Aug 3 '19 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.