These snippets, when added to functions.php, will add functionality to manipulate GravityForms submissions and entries
Update ACF field on User
Will update the value of an ACF field that is attached to the wordpress users form
add_action( 'gform_after_submission_4', 'access_entry_via_field', 10, 2 ); function access_entry_via_field( $entry, $form ) { $area_of_expertise = rgar( $entry, '1' ); $updated = update_user_meta( get_current_user_id(), 'user_profile_fields_group_area_of_expertise', $area_of_expertise ); }
Filters the next, previous and submit buttons
/** * Filters the next, previous and submit buttons. * Replaces the form's <input> buttons with <button> while maintaining attributes from original <input>. * * @param string $button Contains the <input> tag to be filtered. * @param object $form Contains all the properties of the current form. * * @return string The filtered button. */ add_filter( 'gform_next_button', 'input_to_button', 10, 2 ); add_filter( 'gform_previous_button', 'input_to_button', 10, 2 ); add_filter( 'gform_submit_button', 'input_to_button', 10, 2 ); function input_to_button( $button, $form ) { $dom = new DOMDocument(); $dom->loadHTML( '<?xml encoding="utf-8" ?>' . $button ); $input = $dom->getElementsByTagName( 'input' )->item(0); $new_button = $dom->createElement( 'button' ); $new_button->appendChild( $dom->createTextNode( $input->getAttribute( 'value' ) ) ); $input->removeAttribute( 'value' ); foreach( $input->attributes as $attribute ) { $new_button->setAttribute( $attribute->name, $attribute->value ); } $input->parentNode->replaceChild( $new_button, $input ); $classes = $new_button->getAttribute( 'class' ); $classes .= " et_pb_contact_submit et_pb_button"; $new_button->setAttribute( 'class', $classes ); return $dom->saveHtml( $new_button ); } }
Extract checkbox values
function extract_checkbox_values( $gf_form_id, $gf_field_id ) { $field = GFAPI::get_field( $gf_form_id, $gf_field_id ); $value = is_object( $field ) ? $field->get_value_export( $entry ) : ''; $value_array = explode(", ", $value); return $value_array; }
Update an ACF checkbox field with values from a GravityForms checkbox field
add_action( 'gform_after_submission_3', 'add_clinical_research_data', 10, 2 ); function add_clinical_research_data( $entry, $form ) { $user_id = 'user_' . get_current_user_id(); // ACF field name = group-name_field-name $field_key = "user_profile_fields_group_life_sciences_sector_experience"; $field = GFAPI::get_field( 3, 7 ); $value = is_object( $field ) ? $field->get_value_export( $entry ) : ''; $user_profile_fields_group_life_sciences_sector_experience = explode(", ", $value); update_field( $field_key, $user_profile_fields_group_life_sciences_sector_experience, $user_id ); }
Pre-check GravityForms choice field
// https://docs.gravityforms.com/gf_field_checkbox/ // https://docs.gravityforms.com/gform_field_choice_markup_pre_render/ // https://community.gravityforms.com/t/ive-set-isselected-1-on-form-render-but-this-is-not-showing-on-the-form-resolved/13291 // https://stackoverflow.com/questions/60851099/gravity-forms-pre-selected-choices-checkboxes-field add_filter( 'gform_field_choice_markup_pre_render_3_7', function ( $choice_markup, $choice, $field, $value ) { foreach( $form['fields'] as &$field ) { if($field->id == 7) { $options = $field["choices"]; foreach ( $options as $fkey => $option ) { if( $option['value'] == 'Biotechnology'){ $choices[] = array( 'text' => $option['text'], 'value' => $option['value'], 'isSelected' => true ); } else { $choices[] = array( 'text' => $option['text'], 'value' => $option['value'], 'isSelected' => false ); } } $field->choices = $choices; } } return $field; }, 10, 4 );
Populate a GravityForms hidden field with a value
add_filter( 'gform_field_value_cr_user_id', 'fill_cr_user_id' ); function fill_cr_user_id( $value ) { return get_current_user_id(); }
Update a users data when they register
Do somethng (update an ACF field) when a new user registers (perhaps via a membership plugin)
add_action( 'user_register', 'myplugin_registration_save', 10, 1 ); function myplugin_registration_save( $user_id ) { $user_info = get_userdata($user_id); $username = $user_info->user_login; $value = '/member-profile/' . $username . '/'; update_field( 'field_6501995abb09a', $value, 'user_' . $user_id ); }
Function to sanitize a URL from a file upload field
function sanitize_gf_url($url) { $url = ltrim($url, '["'); $url = rtrim($url, '"]'); if ( !empty($url) ) { $url = sanitize_url($url); $url_filename = basename($url); return '<a href="' . $url . '" target="_blank">' . $url_filename . '</a>'; } else { return '<strong>No files found</strong>. Please review your <a href="/edit_profile/?#tabs|1">form entry</a> and upload the relevant file in PDF format.'; } }
Search GravityForms Entries
Shortcode to search for entries created by the currently logged in user. Used in combination with GP Easy Passthrough to edit the users existing entry, rather then create a new entry on form submission
add_shortcode( 'show_certs', 'show_certs_func' ); function show_certs_func( $atts ) { $form_id = 21; $entries = GFAPI::get_entries($form_id, array('created_by' => get_current_user_id(), 'status' => 'active' )); if (!empty($entries)) { foreach ($entries as $entry) { $out = '<p><strong>Training Cert:</strong></p><p>' . sanitize_gf_url($entry['47']) . '</p>'; $out .= "<p><strong>PI Insurance:</strong></p><p> " . sanitize_gf_url($entry['43']) . "</p>"; $out .= "<p><strong>PL Insurance:</strong></p><p> " . sanitize_gf_url($entry['45']) . "</p>"; $out .= "<p><strong>Other relevant documentation:</strong></p><p> " . sanitize_gf_url($entry['49']) . "</p>"; } } else { return '<p><strong>No files found</strong>. Please review your <a href="/edit_profile/?#tabs|1">form entry</a> and upload the relevant file in PDF format.</p>'; } return $out; }