Skip to content

Instantly share code, notes, and snippets.

@fencermonir
Created March 21, 2020 13:34
Show Gist options
  • Save fencermonir/f8e5e18f753e84de7c292e583100083c to your computer and use it in GitHub Desktop.
Save fencermonir/f8e5e18f753e84de7c292e583100083c to your computer and use it in GitHub Desktop.
Dynamically add product on CF7 select tag.
<?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