Support

Account

Home Forums General Issues How to populate a post object (multiple, with Gravity forms)

Solved

How to populate a post object (multiple, with Gravity forms)

  • I have created a form with Gravity forms which in turn creates a user. With ACF I created multiple custom fields in each user which also has a post object field. All my fields work fine, except the post object field.

    In my form I have a checkbox field which can hold multiple items, and they are exactly the same as the titles of my posts.

    I have contacted GF and they told me to serialized the data (docs: https://docs.gravityforms.com/gform_user_registration_prepared_value/)

    And I tried to modify the following code snippet, but nothing seems to work. How can I convert the data from GF checkboxes so that the ACF posts object understands my data? Do I need slug, post IDs, do I need more than just one value? In the past I have filled the post object also with other means than the WP UI (via import tools) and then giving the title of the post is fine.

    add_filter( 'gform_user_registration_prepared_value', 'gf_serialize_checkboxes', 10, 5 );
     
    function gf_serialize_checkboxes( $value, $field, $input_id, $entry, $is_username ) {
     
        if ( $field->id == '18' && $field->type == 'checkbox' ) { // Update 18 to your field id number
            // Get a comma separated list of checkboxes checked
            $checked = is_object( $field ) ? $field->get_value_export( $entry ) : '';
     
            // Convert to array
            $values = explode(",", $checked);
     
            // Serialize the data
            $value = maybe_serialize( $values );
        }
        return $value;
    }
  • Would you be willing to share your code for how you made the rest of your fields work? I am struggling to even get that far with my User Registration GF to even populate my user Advanced Custom Fields.

  • I had changed the fields Return Format from Post Object to Post ID and now some of the code works! The only issue is that it will only save the first item that is checked and all item after that will be ignored.

  • Fixed my single data issue also. The data I got back from my previous function looked like

    a:6:{i:0;s:3:"112";i:1;s:4:" 113";i:2;s:4:" 114";i:3;s:4:" 115";i:4;s:4:" 111";i:5;s:4:" 116";}

    Take a note to the spaces, the spaces resulted in incompatible data. To fixed this I changed the line in the function $values = explode(",", $checked); to $values = explode(", ", $checked); and now the data looked like:

    a:6:{i:0;s:3:"114";i:1;s:3:"113";i:2;s:3:"116";i:3;s:3:"115";i:4;s:3:"111";i:5;s:3:"112";}

    No spaces!

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

The topic ‘How to populate a post object (multiple, with Gravity forms)’ is closed to new replies.