Home Forums ACF PRO Integrate an acf_form into an existing form


Integrate an acf_form into an existing form

  • I think, I am missing something stupidly simple. Assuming the following situation: I have a frontend form, where a user is allowed to change his WP credentials. Depending on a hidden form field named ss_login_action, I trigger some different actions of my plugin. On init, I check the field like so:

    // check for $_POST
    $action = filter_input( INPUT_POST, 'ss_login_action', FILTER_SANITIZE_STRING );
    	// check for $_GET, if $action not yet is set via $_POST
    	$action = filter_input( INPUT_GET, 'ss_login_action', FILTER_SANITIZE_STRING );

    Works like a charm so far, all form data is successfully updated. But here comes the trouble: If a user fulfills some conditions, I allow him to edit some others post data as well. To do so, I integrate an acf_form via an action hook in my plugin this way:

    public function cpt_people_data_fields($user) {
    		 $cpt_people_id = intval(get_user_meta($user->ID, 'cpt_people_id', true));
    		 if($cpt_people_id > 0) {
    			// there is a cpt assigned to this user 
    			echo sprintf( '<h4>%s</h4>', 'Deine gespeicherten Mitgliedsdaten' );
    			echo '<fieldset>';
    			$options = array(
    				'post_id' => $cpt_people_id,
    				'fields' => array('titel','geburtsname','rufname','geburtsdatum','anschrift','plz','ort','telefon','telefon_mobil'),
    				'form' => false,
    				'instruction_placement' => 'field',
    			echo '</fieldset>';

    The field of the acf_form are saving like expected. But when I integrate this acf_form, my $action variable from above is always NULL. So the normal user fields are not saved, because my plugin does not know which action to trigger.

    I am banging my head against my desk for hours now 😉 Does anybody have an idea how to get my $action populated again? The return parameter is of no use as far as I can see, since I need the $_POST data of the other fields as well. Every bit of help is gladly appreciated.

    Greetings from Germany

  • I tried to change the form method to get and my check of the $_POST to $_REQUEST. In this case, only the content of my normal form is updated, but not the acf_form fields. At first glance, I have a redirect or priority problem. Help anyone?

  • Hi @sixtyseven

    Have you tried using the acf/save_post action to save your information?

    I think you need to use $_POST if you want your ACF values to work properly.
    You say you have a redirect issue? What are you basing that on? Have you looked at the rendered forms markup to see that there’s nothing clashing there like duplicate inputs etc.

  • It’s a sort of a workaround, but it works. Thank you for your help. Could you probably tell me how to add another query variable to the acf redirect on the fly? I need some user feedback regarding the non acf fields. Something like

    $url = add_query_arg( 'ss_login_msg', 'update_emailempty', $url );

    But before the acf redirect kicks in. Ideas about that?

  • But if you’re adding the ACF to your own form no ACF redirection would occur?

    I haven’t tried this myself that’s why I need to ask 🙂

  • Again you brought me on the trail of something 😉 acf_form redirects to the page where it was called on with a $_GET parameter of ?updated=true by default. To handle this on my own, I had to explicit set ‘return’ => ” in the options array of the acf_form.

    The longer I work with acf, the deeper I get in. And my combined form works like a charm now.

    Thanks for the support!

  • Ah I see.. I thought that if the form parameter was set to false ACF would not attempt any redirection at all. Something new learnt every day!

    Great to hear you got it working flawlessly. Sometimes you just need a ball plank to bounce thoughts on.

    Feel free to open up a new topic if you need further help.
    Have a nice day

  • Hi there,

    I have an existing form where I would like to add my ACF fields to it.
    Somehow I can’t get it work. Can someone give me a hint on how to start with it?

    Many thanks in advance…
    Best regards

  • Hi Fireleaf,

    You can add your ACF fields to an existing form by setting the “form” parameter to false.

    <?php acf_form( array(
    'form' => false
    ) ); ?>

    You would still need to add the <?php acf_form_head(); ?> to the top of your template tho (before get_header()).

  • I have similar problem. I have a form, and a function processing it somewhere in the page (usual way, no save_post or pre_save_post).
    After adding acf_form (with form=>false, it’s only for 2-3 fields) i’m getting empty $_POST array printed on my page, without acf_form, everything works properly.
    There is some kind of redirect issue, but i don’t know how to stop it from redirecting, should i do something with acf/save_post?

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

The topic ‘Integrate an acf_form into an existing form’ is closed to new replies.