I found another post that had the code for dynamically populating a CF7 dropdown with the terms of a taxonomy, and I have it working, but I wanted the terms to not just be all within the taxonomy, but I wanted them to only be relative to the post that it's on.
To give it some more context, this is an application form on a job posting. The custom post type are job listings. I have a taxonomy of locations. Those terms are assigned to a post. I want only the selected terms to appear in this dropdown.
This is the code I have:
function dynamic_select_list( $tag ) {
// Only run on select lists
if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
return $tag;
} else if ( empty( $tag['options'] ) ) {
return $tag;
}
$term_args = array();
// Loop thorugh options to look for our custom options
foreach( $tag['options'] as $option ) {
$matches = explode( ':', $option );
if( ! empty( $matches ) ) {
switch( $matches[0] ) {
case 'taxonomy':
$term_args['taxonomy'] = $matches[1];
break;
case 'parent':
$term_args['parent'] = intval( $matches[1] );
break;
}
}
}
// Ensure we have a term arguments to work with
if( empty( $term_args ) ) {
return $tag;
}
// Merge dynamic arguments with static arguments
$term_args = array_merge( $term_args, array(
'hide_empty' => false,
) );
$terms = get_terms( $term_args );
// Add terms to values
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
$tag['values'][] = $term->name;
}
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );
I found another post that had the code for dynamically populating a CF7 dropdown with the terms of a taxonomy, and I have it working, but I wanted the terms to not just be all within the taxonomy, but I wanted them to only be relative to the post that it's on.
To give it some more context, this is an application form on a job posting. The custom post type are job listings. I have a taxonomy of locations. Those terms are assigned to a post. I want only the selected terms to appear in this dropdown.
This is the code I have:
function dynamic_select_list( $tag ) {
// Only run on select lists
if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
return $tag;
} else if ( empty( $tag['options'] ) ) {
return $tag;
}
$term_args = array();
// Loop thorugh options to look for our custom options
foreach( $tag['options'] as $option ) {
$matches = explode( ':', $option );
if( ! empty( $matches ) ) {
switch( $matches[0] ) {
case 'taxonomy':
$term_args['taxonomy'] = $matches[1];
break;
case 'parent':
$term_args['parent'] = intval( $matches[1] );
break;
}
}
}
// Ensure we have a term arguments to work with
if( empty( $term_args ) ) {
return $tag;
}
// Merge dynamic arguments with static arguments
$term_args = array_merge( $term_args, array(
'hide_empty' => false,
) );
$terms = get_terms( $term_args );
// Add terms to values
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
$tag['values'][] = $term->name;
}
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );
Share
Improve this question
asked Feb 15, 2020 at 23:33
Darren BachanDarren Bachan
3752 gold badges7 silver badges17 bronze badges
4
|
1 Answer
Reset to default 1Use the dynamic_dropdown tag functionality offered by the Smart Grid layout extension. The dynamic tag can take 3 sources of data (a given taxonomy, or branch within that taxonomy, a list of post titles, or the results of a filtered hook function). Use the filter hook to populated your dropdown as,
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
if($form_key != 'my-form') return $options; //check this is the correct form.
if($field_name != 'custom-dropdown') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_terms( $term_args );
// Add terms to values
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->id] = $term->name;
}
}
return $options;
}
$terms = get_terms( $term_args );
with$terms = get_the_terms( $post->ID, 'location_categories' );
and it works. – Darren Bachan Commented Feb 16, 2020 at 18:20