Home › Toolset Professional Support › [Resolved] Gravity Form checkboxes saving to custom field checkboxes
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 |
---|---|---|---|---|---|---|
- | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 14:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Jamaica (GMT-05:00)
This topic contains 19 replies, has 3 voices.
Last updated by Purchasing WCER 3 years, 4 months ago.
Assisted by: Shane.
On this page: hidden link there is a Gravity Form that has fields mapped to custom toolset fields using the Gravity Forms post creation add-on. Add the fields working for the "activity" post creation with the exception of the checkbox fields.
I've been working with Gravity Forms on the code but I'm not certain I have the correct "custom meta key". Here, https://toolset.com/forums/topic/help-with-meta-query-with-toolset/ it says the key should be the same as the custom field slug?
And my understanding is toolset stores checkboxes as a serialized array. Is that correct?
This is the code I'm using that does not work. Nothing records in the checkboxes of the posts. I need this code to work for both fields 5 and 7 on the form. Grade Levels (ID 5) and National Standard (ID 7).
//serialize Gravity Forms checkboxes for Submit an Activity Gravity form - Form 1 Field ID 5
add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 5;
// 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, 'wpcf-grade-levels', $values );
}
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Yes, the checkboxes field is stored as a serialized array, but it is still in a proprietary format, so your code will not work.
We don't have an API function for programmatically setting checkboxes field values either, so in principle what you want is simply not possible.
However, we in support have produced our own unofficial API function to set or unset checkboxes fields values.
You can see details of that here, and you are welcome to try and incorporate it into your own custom code:
hidden link
Hi Nigel,
Thank you. I'm glad to hear there may be some hope of getting this working but I'm not sure how to implement this with the Gravity Form I'm using. This code goes in the functions.php file but how do I make the connection to the Gravity Form? Am I reading this correctly, that each checkbox option has to be set individually?
Here is the info on the form
Form ID: 1
IDs of fields that are checkboxes: 5, 7
custom field slugs in toolset: grade-levels, standard
Grade-levels options: K-2, 3-5, 6-8, 9-12
National standard options: Psychomotor, Cognitive, Affective
Toolset custom post type slug: activity
As an aside I really wish the Toolset post form allowed required fields and had better error handling. Is that on the feature list?
Hi Dung,
Based on your example you will only need to call the function ts_checkboxes() with the value and state of your field.
In simple terms like below.
//serialize Gravity Forms checkboxes for Submit an Activity Gravity form - Form 1 Field ID 5 add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 ); function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) { // Checkboxes field id. $field_id = 5; // Get field object. $field = GFAPI::get_field( $form, $field_id ); if ( $field->type == 'checkbox' ) { ts_checkboxes( $post_id, 'Agrade-levels', 'K-2', 'check' ); } }
What you're going to have to do is to check the state of each of the checkboxes options on gravity forms and then use the ts_checkboxes() function to check or uncheck that option based on its current state.
With this unofficial API function you will need to check and uncheck the values individually. Given that you should be able to get the checked options from Gravity form, you can pick out the states of each of the options and set them accordingly using the ts_checkboxes function.
Please let me know if this provides any clarity for you.
Thanks, Shane. Unfortunately, when I add that code the form no longer submits, it just spins forever.
I tried your code and this code and both had the same result of stopping the form from submitting.
//serialize Gravity Forms checkboxes for Submit an Activity Gravity form - Form 1 Field ID 5
add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 5;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'checkbox' ) {
ts_checkboxes( $post_id, 'Agrade-levels', 'K-2', 'check' );
ts_checkboxes( $post_id, 'Agrade-levels', '3-5', 'check' );
ts_checkboxes( $post_id, 'Agrade-levels', '6-8', 'check' );
ts_checkboxes( $post_id, 'Agrade-levels', '9-12', 'check' );
}
}
Hi Dung,
There must be a fatal error thrown somewhere to be causing the infinite loading.
Do you have access to your servers php logs ? If so can you check to see if there are any fatal errors being generated there?
Also just to confirm have you added the function code that nigel had provided in our Knowledge Base link below ?
hidden link
Before you can call the ts_checkboxes() function you must first add the code for it to your Toolset custom code settings or your functions.php file
Please let me know.
Thanks,
Shane
Hi shane,
My server admin is out for the next week so I won't be able to get server log info until then.
I did not add the KB code in addition to the code Nigel sent. Is this what should go into the functions.php file? Should some of the variables in the KB code be changed? This is what I tried - all the in the functions.php file. I'm sure I don't have everything set correctly. It still just spins.
/**
* Unofficial API function to check/uncheck checkboxes options
*
* @param int $post_id
* @param string $field // slug of checkboxes field
* @param string $option // title of checkbox option to manipulate
* @param string $action : 'check' | 'uncheck' | 'value' (default, returns current value)
*
* Important: assumes recommended checkboxes setting of save nothing to database when unchecked
*/
function ts_checkboxes( $post_id, $field, $option, $action = 'value' ){
if ( isset($post_id) && isset($field) && isset($option) ){
$field_settings = types_get_field( $field );
$field_options = $field_settings['data']['options'];
// Locate the option key
$key = array_search( $option, array_column( $field_options, 'K-2' ) );
$keys = array_keys( $field_options );
$option_key = $keys[$key];
// Get the current post meta value
$meta = get_post_meta( $post_id, 'wpcf-grade-levels'.$field, true );
// If action = 'value' just return the value
if ( $action == 'value' && isset( $meta[$option_key] ) ){
return $meta[$option_key][0];
} else {
// Remove the existing key if it exists (i.e. uncheck)
// because recommended setting is to save nothing when unchecked
unset( $meta[$option_key] );
// If $checked == true then add back the key + value
if ( $action == 'check' ){
$meta[$option_key] = array($field_options[$option_key]['set_value']);
}
update_post_meta( $post_id, 'wpcf-grade-levels'.$field, $meta );
}
}
}
//serialize Gravity Forms checkboxes for Submit an Activity Gravity form - Form 1 Field ID 5
add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 5;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'checkbox' ) {
ts_checkboxes( $post_id, 'grade-levels', 'K-2', 'check' );
ts_checkboxes( $post_id, 'grade-levels', '3-5', 'check' );
ts_checkboxes( $post_id, 'grade-levels', '6-8', 'check' );
ts_checkboxes( $post_id, 'grade-levels', '9-12', 'check' );
}
}
Hi Dung,
Would it be possible to get access to the site to help further with this ?
I've enabled the private fields for your next response. Please remember that this is a custom function built by us in support to assist customers so I cannot guarantee that it will function 100% for your case.
However if an error is being thrown that we can identify. It should be able to let us know what the issue is.
Thanks,
Shane
Hi Dung,
Thank you for the credentials, however when I test them they don't seem to work.
Can you check to see if they work for you ?
Thanks,
Shane
Can you reactivate the fields? I need to give you an email address instead of the username.
Hi Dung,
Here are the private fields.
Hi Dung,
Thank you for the credentials.
I had a look and saw that I need access to the logs in order to see what is wrong. Unfortunately we will have to wait until your server admin returns to provide us with the logs to see where or what is causing the functions to fail.
Thanks,
Shane
There's nothing in the error.log file for the site itself from later than December of 2020.
The apache log has this:
[Tue Jun 22 14:20:49.839838 2021] [php7:error] [pid 24992] [client 63.143.95.78:17463] PHP Fatal error: Uncaught Error: Cannot unset string offsets in
/var/www/wordpress/ediphy/wp-content/themes/oceanwp-child/functions.php:77
Stack trace:
#0 /var/www/wordpress/ediphy
/wp-content/themes/oceanwp-child/functions.php(103): ts_checkboxes()
#1 /var/www/wordpress/ediphy/wp-includes/class-wp-hook.php(294):
apc_serialize_checkboxes()
#2 /var/www/wordpress/ediphy/wp-includes/class-wp-hook.php(316):
WP_Hook->apply_filters()
#3 /var/www/wordpress/ediphy/wp-includes/plugin.php(484): WP_Hook->do_action()
#4
/var/www/wordpress/ediphy/wp-content/plugins/gravityforms/gravityforms.php(6860):
do_action()
#5
/var/www/wordpress/ediphy/wp-content/plugins/gravityformsadvancedpostcreation/class-gf-advancedpostcreation.php(1927):
gf_do_action()
#6
/var/www/wordpress/ediphy/wp-content/plugins/gravityformsadvancedpostcreation/class-gf-advancedpostcreation.php(1779):
GF_Advanced_Post_Creation->create_post()
#7
/var/www/wordpress/ediphy/wp-content/plugins/gravityforms/includes/addon/class-gf-feed-addon.php(361):
GF_Advanced_Post_Creation->p in
/var/www/wordpress/ediphy/wp-content/themes/oceanwp-child/functions.php on line 77, referer: hidden link
Hi Dung,
It would appear that the custom snippet is throwing a fatal error which is causing the form to break.
Is there a particular reason why you aren't using our Toolset Forms plugin to create this post ? Given that submitting checkbox with our Forms works out of the box?
Secondly seeing that the custom snippet is actually created by one of our supporters then I wouldn't be able to debug it as this is out of the scope of our support forum.
We can only debug the official API functions as well as pass on the report to the developers for them to fix it.
Thanks,
Shane
That's frustrating. I started off using a toolset post form - you will see it in the site under post forms but there is no way to set fields as required and scripts found in other support tickets didn't work to do so. Error handling is also lacking. The form isn't very useful when the only field that is required is the title. Would love to know if there is a way to fix that or if it is on something you all are working on.