Last active
March 7, 2025 16:08
-
-
Save fgrweb/7a16100782f81198bb49a00b02a41e98 to your computer and use it in GitHub Desktop.
Contact Form 7. Populate checkboxes field with ACF multiselect field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * We need to add the data to the CF7 field shortcode. | |
| * This example is with a data called 'datos-categorias'. | |
| * The shortcode in CF7 will be something like | |
| * [checkbox product-categories use_label_element data:datos-categorias] | |
| */ | |
| add_filter( 'wpcf7_form_tag_data_option', 'fgrweb_populate_cf7', 10, 3 ); | |
| /** | |
| * Populate CF7 dynamic fields. | |
| * | |
| * @param array $n The data. Always null. | |
| * @param array $options The options. | |
| * @param string $args The arguments. Always empty string. | |
| * @return array|null The populated categories or null if none. | |
| */ | |
| function fgrweb_populate_cf7( $n, $options, $args ) { | |
| // product categories in options. | |
| if ( in_array( 'datos-categorias', $options ) ) { | |
| $cat_checkboxes = array(); | |
| $product_categ = get_field( 'categorias_productos_formulario', 'option' ); | |
| if ( ! empty( $product_categ ) ) { | |
| $options = array(); | |
| foreach ( $product_categ as $categ ) { | |
| // get category name by id. | |
| $cat_name = get_term_by( 'id', $categ, 'product_cat' )->name; | |
| $cat_checkboxes[] = $cat_name; | |
| } | |
| } | |
| return $cat_checkboxes; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment