Home / Admin / Mapping WPForms Checkbox Values to ACF Checkbox Fields
Duplicate Snippet

Embed Snippet on Your Site

Mapping WPForms Checkbox Values to ACF Checkbox Fields

This snippet uses the wpforms_post_submissions_process action hook to dynamically map WPForms checkbox submissions to ACF checkbox field keys.

<10
Code Preview
php
<?php
/**
Update ACF age field based on WPForms checkbox submission.
@param  int      $post_id     Post ID.
@param  array    $fields      Sanitized entry field values/properties.
@param  array    $form_data   Form settings/data.
@param  int      $entry_id    Entry ID.
@return void
 */
function wpf_dev_update_age_field_from_submission( $post_id, $fields, $form_data, $entry_id ) {
 // Only process form #83
 if ( absint( $form_data['id'] ) !== 83 ) {
 return;
 }
 // Configuration
 $wpforms_checkbox_field_id = 1;
 $acf_field_name = 'age';
 // Get ACF field object to retrieve choices dynamically
 $acf_field = get_field_object( $acf_field_name );
 if ( ! $acf_field || ! isset( $acf_field['choices'] ) ) {
 return;
 }
 // Build mapping from ACF choices (label => value)
 $value_map = array();
 foreach ( $acf_field['choices'] as $value => $label ) {
 $value_map[$label] = $value;
 }
 // Get selected values from WPForms submission
 $acf_values = array();
 if ( isset( $fields[$wpforms_checkbox_field_id] ) && 
  $fields[$wpforms_checkbox_field_id]['type'] === 'checkbox' && 
  ! empty( $fields[$wpforms_checkbox_field_id]['value'] ) ) {
 
 // Split checkbox values by newline
 $selected_values = explode( "\n", $fields[$wpforms_checkbox_field_id]['value'] );
 
 // Map WPForms values to ACF values
 foreach ( $selected_values as $value ) {
     $value = trim( $value );
     if ( isset( $value_map[$value] ) ) {
         $acf_values[] = $value_map[$value];
     }
 }
 }
 // Update ACF field
 if ( function_exists( 'update_field' ) ) {
 update_field( $acf_field_name, $acf_values, $post_id );
 }
}
add_action( 'wpforms_post_submissions_process', 'wpf_dev_update_age_field_from_submission', 10, 4 );

Comments

Add a Comment