Support

Account

Home Forums ACF PRO Add a field from the ACF Pro in the form WP User Frontend Pro

Solving

Add a field from the ACF Pro in the form WP User Frontend Pro

  • Hello!
    I created a box with the type of Flexible Content Field in the Advanced Custom Fields Pro and want to add it in the form WP User Frontend Pro, that I made.
    In the form WP User Frontend Pro I added a field Action Hook Field in functions.php file code:

    function flexible_field_form( $form_id, $post_id, $form_settings) {
    
      $new_post = array(
        // PUT IN YOUR OWN FIELD GROUP ID(s)
        'field_groups'       => array(52), // Create post field group ID(s)
        'form' => false
      );
      acf_form( $new_post );
      
      // insert the post
      $post_id = wp_insert_post( $post );
    
      // Save the fields to the post
      do_action( 'acf/save_post' , $post_id );
    
    }
    
    add_action( 'flexible_field_hook', 'flexible_field_form', 10, 3 );

    Well, in the field setting Action Hook Field put the name of the hook flexible_field_hook.

    Under Connect and data added to the form, but unfortunately does not store data, ie, You need to add a record ID.

    Of course I have created a pure form of the addition of the ACF, but minus one, that the simplicity and ease of customization on the side of the WP User Frontend Pro, and with entry form editing much easier through the Fronde-end than at the ACF.

    Thank you.

  • Hi @ingwine

    I believe you need to get the posted data manually using the $_POST["acf"]["field_1234567890"] where “field_1234567890” is the field key of the flexible field. You can update the field using update_field().

    I hope this helps.

  • Thank you for your response, but you can explain more, or show an example of my code?

  • Hi @ingwine

    I’m not sure how WP User Frontend Pro works, but I believe you can do it like this:

    function flexible_field_form( $form_id, $post_id, $form_settings) {
    
        $new_post = array(
            // PUT IN YOUR OWN FIELD GROUP ID(s)
            'field_groups'       => array(52), // Create post field group ID(s)
            'form' => false
        );
        acf_form( $new_post );
    
        // insert the post
        $post_id = wp_insert_post( $post );
        $field_key = "field_1234567890";
        $posted_flexible = $_POST["acf"][$field_key];
        
        $value =  (get_field($field_key) ? get_field($field_key) : array());
        foreach($posted_flexible as $layout){
            $value[] = array("sub_field_1" => $layout['field_1111111111'], "sub_field_2" => $layout['field_2222222222'], "acf_fc_layout" => $layout["acf_fc_layout"]);
        }
        update_field( $field_key, $value, $post_id );
    }
    
    add_action( 'flexible_field_hook', 'flexible_field_form', 10, 3 );

    When you post some Data from ACF form, you can get them using $_POST["acf"].

    I hope this makes sense.

  • Show error

    Warning: Invalid argument supplied for foreach() in /home/users/test.com/wp-content/themes/mytheme/functions.php on line 630

    line 630:

    foreach($posted_flexible as $layout){

    I found this question – https://wedevs.com/support/topic/readingediting-acf-fields-with-wp-user-frontend/

    But it is unclear how this method of addition of ACF in the WP User Frontend, so I started using http://docs.wedevs.com/docs/wp-user-frontend-pro/developer-docs/action-hook-field/

    UPD:

    I replace flexible type on text type field, does not work.

    function flexible_field_form( $form_id, $post_id, $form_settings) {
    
        $new_post = array(
            // PUT IN YOUR OWN FIELD GROUP ID(s)
            'field_groups'       => array(116), // Create post field group ID(s)
            'form' => false
        );
        acf_form( $new_post );
    
        // insert the post
        $post_id = wp_insert_post( $post );
        $field_key = "field_56d32aaeb2284";
        $posted_flexible = $_POST["acf"][$field_key];
        
        $value =  get_field($field_key);
        update_field( $field_key, $value, $post_id );
    }
    
    add_action( 'flexible_field_hook', 'flexible_field_form', 10, 3 );

    Field does not load the data into a form, and does not add.

  • Hi @ingwine

    Could you please var_dump() the $_POST[“acf”] variable?

    Basically, you need to add the acf_form( $new_post ); to where you want it to show up and then get the posted data when the form is saving the data. It’s a bit weird that you can set it on the same hook. But again, I’m not familiar with WP User Frontend Pro. I’m sorry about that.

    I think you can ask the plugin author where you can get the posted data from the form using the $_POST variable and the new post ID. This way you can update it easily.

    You can also check if the form posted the data by using the developer tools from your browser (I use Google Chrome’s developer tool). I’ve attached a screenshot for your reference.

    I hope this helps.

  • var_dump($_POST["acf"][$field_key]);

    Results:

    NULL

  • Hi @ingwine

    Could you please var_dump() the $_POST variable?

    If it doesn’t have the form data, it means it isn’t the right place to update the post. Could you please ask the plugin author about this case? Maybe they can lead us to the right direction 🙂

    Thanks!

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

You must be logged in to reply to this topic.