Skip Navigation

[Resolved] Save checkboxes in Gravity Forms to custom taxonomy or custom field in Toolset

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1978757

I need to save values from checkboxes in gravity forms to either a custom taxonomy or custom checkbox fields in Toolset. I've treid both and the code here but no success. https://toolset.com/forums/topic/gravity-forms-types-checkbox-mapping-for-use-meta/

What is the link to your site? hidden link

Debug and admin credentials are in my last ticket with Shane.

// capture a GF checkbox field values and serialize so WP Types CPT can display checked properly
add_action("gform_after_submission", "add_custom_post_meta", 10, 2);
function add_custom_post_meta($entry, $form) {
$arr = array(
array('research-area', '21'),
array('poster-topic', '22'),
array('research-area', '28'),
);
foreach($arr as $v){
$items = get_checkbox_value( $entry, $v[1] );
$value = my_checkboxes_func($items, $v[0]);
update_post_meta($entry['post_id'], 'wpcf-' . $v[0], $value);
}
}

function get_checkbox_value( $entry, $field_id ){

//getting a comma separated list of selected values
$lead_field_keys = array_keys( $entry );
$items = array();
foreach ( $lead_field_keys as $input_id ) {
if ( is_numeric( $input_id ) && absint( $input_id ) == $field_id ) {
$items[] = GFCommon::selection_display( rgar( $entry, $input_id ), null, $entry['currency'], false );
}
}
//$value = GFCommon::implode_non_blank( ', ', $items );

return $items;
}

function my_checkboxes_func($items = array(), $types_field = '') {
$fields = WPCF_Fields::getFields();
$arr = array();
if(isset($fields[$types_field]['data']['options'])){
foreach ($fields[$types_field]['data']['options'] as $k=> $v){
if(in_array($v['set_value'], $items)){
$arr[$k] = array($v['set_value']);
}
}
}
return $arr;
}

#1978833

Hello, it would probably be best for me to make a clone of your site so I can troubleshoot this code in my local environment. Your admin credentials are removed from older tickets once they are resolved, so I'll need you to resubmit login credentials in the private reply fields here. Once I have the login credentials, I will make a clone of your site using the All-in-One WP Migration plugin and install that site clone in my local environment, then give you some feedback.

#1978849

Thanks, I was able to log in. When I try to install the plugin, I am asked for additional credentials (FTP or SSH). It might be easiest if you upload the plugin for me: https://wordpress.org/plugins/all-in-one-wp-migration/

Can you upload the plugin so I can create the site clone?

#1978887

The plugin is uploaded

#1978889

The plugin is installed and activated.

#1978897

Thank you, I have created the site clone. You may delete the plugin at your convenience. I will run some tests and give you an update as soon as I can. My day is closing here soon so it may be tomorrow before I have completed the updates.

#1979775

I'm a bit stuck on a GravityForms API issue. In order to update a custom field value in the new Poster post created by GravityForms, I need to know the new post ID. It seems I should be able to access that in the code here, based on the documentation for gform_after_submission:

add_action("gform_after_submission", "add_custom_post_meta", 10, 2);
function add_custom_post_meta($entry, $form) {
  $post_id = $entry['post_id'];

...however, $entry['post_id'] is always blank. If I dump the $entry variable:

add_action("gform_after_submission", "add_custom_post_meta", 10, 2);
function add_custom_post_meta($entry, $form) {
  error_log(print_r($entry, true));

I get something like this in the logs:

Array
(
    [id] => 47
    [status] => active
    [form_id] => 2
    [ip] => ::1
    [source_url] => <em><u>hidden link</u></em>
    [currency] => USD
    [post_id] =>
    [date_created] => 2021-03-09 14:15:58
    [date_updated] => 2021-03-09 14:15:58
    [is_starred] => 0
    [is_read] => 0
    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36
    [payment_status] =>
    [payment_date] =>
    [payment_amount] =>
    [payment_method] =>
    [transaction_id] =>
    [is_fulfilled] =>
    [created_by] => 349
    [transaction_type] =>
    [19] => WCER Admin
    [7] => ********@gmail.com
    [20] =>
    [12] =>
    [9] => qwer
    [14] =>
    [10] =>
    [11] => qwetafg
    [31] => <em><u>hidden link</u></em>|:||:||:|
    [30] =>
    [22.1] =>
    [22.2] =>
    [22.3] =>
    [22.4] =>
    [22.5] =>
    [22.6] =>
    [22.7] =>
    [22.8] =>
    [22.9] =>
    [22.11] =>
    [22.12] =>
    [22.13] =>
    [22.14] =>
    [22.15] =>
    [22.16] =>
    [22.17] =>
    [28.1] => Early Childhood
    [28.2] => Graduate Education
    [26] =>
    [21.1] =>
    [21.2] =>
    [21.3] =>
    [21.4] =>
    [21.5] =>
    [21.6] =>
    [32] =>
)

You can see post_id is blank. I'm not very familiar with Gravity Forms APIs, so I'm not sure what the problem could be. I've tried increasing the priority:

add_action("gform_after_submission", "add_custom_post_meta", 99, 2);

No luck. I've tried turning off AJAX in the Gravity Forms shortcode:

[gravityform id="2"]

No luck. I've tried turning off all plugins except the Gravity Forms plugins and the Toolset plugins, no luck. I've tried changing the post status of the Poster post in the Form settings, but no luck. So I'm stuck. Without being able to access the new post ID, I'm not able to set custom field values programmatically. Maybe gform_after_submission is not the best hook for this case? I'm not sure. Perhaps it has something to do with the Gravity Forms + Custom Post Types plugin, or the Gravity Forms Advanced Post Creation Add-On?

If you show me how to access the new post ID successfully, I can continue.

#1981103

Thanks, Christian. I am checking with Gravity Forms support on the ID and hook questions. I'll let you know when I hear back from them.

#1981117

Thank you, I will stand by for your update.

#1981433

This is what I recieved from Gravity Forms Support:

"Hook into the action gform_advancedpostcreation_post_after_creation. This will give you easy access to the Post ID of the post that has been created by your form – along with the Feed Object, Entry Object, and Form Object associated with the submission. hidden link

I'm not sure exactly how Toolset structures data, but of special interest might be the example on that page to Update a post custom field with serialized GF checkboxes. hidden link"

Based on this I added the code below which seems to work. Also had to uncheck the "populate with taxonomy" checkbox on the form fields in Gravity Forms so that the value of the checkboxes could be set to match the taxonomy name (not the slug). Just wanted to note this in case it is helpful to others.

//serialize Gravity Forms checkboxes for Poster Fair form
add_filter( 'gform_advancedpostcreation_post_after_creation_2', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {

// Checkboxes field id.
$field_id = 21;
$field_id = 22;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );

if ( $field->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );

// Convert to array.
$values = explode( ', ', $checked );

}

// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'my_custom_field_key', $values );
}

?>

#1981453

Great, thanks for sharing your solution for other clients. Feel free to open new tickets here as needed. I'm closing out here.