Created
March 21, 2020 13:34
-
-
Save fencermonir/f8e5e18f753e84de7c292e583100083c to your computer and use it in GitHub Desktop.
Dynamically add product on CF7 select tag.
This file contains 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
<?php | |
/** | |
* add product in cf7 select option | |
* help: https://pineco.de/dynamic-select-list-in-contact-form-7/ | |
*/ | |
function aggregate_dynamic_product_field_values ( $scanned_tag, $replace ) { | |
if ( $scanned_tag['name'] != 'aggregate-product' ) | |
return $scanned_tag; | |
$rows = get_posts( | |
array ( | |
'post_type' => 'aggregate_product', | |
'numberposts' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC' | |
) | |
); | |
if ( ! $rows ) | |
return $scanned_tag; | |
foreach ( $rows as $row ) { | |
$scanned_tag['raw_values'][] = $row->post_title . '|' . $row->ID; | |
} | |
$pipes = new WPCF7_Pipes($scanned_tag['raw_values']); | |
$scanned_tag['values'] = $pipes->collect_afters(); | |
$scanned_tag['labels'] = $pipes->collect_befores(); | |
$scanned_tag['pipes'] = $pipes; | |
return $scanned_tag; | |
} | |
add_filter( 'wpcf7_form_tag', 'aggregate_dynamic_product_field_values', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment