Last active
August 13, 2024 16:44
-
-
Save morgyface/56474f0a37abb7d622880daf6eff6e40 to your computer and use it in GitHub Desktop.
WordPress | Contact Form 7 | Custom select from post list
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
<?php | |
// Custom contact form 7 retreat select | |
add_action( 'wpcf7_init', 'custom_retreat_select' ); | |
function custom_retreat_select() { | |
wpcf7_add_form_tag( 'retreat_select', 'custom_retreat_handler', array( 'name-attr' => true ) ); | |
} | |
function custom_retreat_handler( $tag ) { | |
$atts = array(); | |
$atts['name'] = $tag->name; | |
$atts['class'] = $tag->get_class_option(); | |
$atts['id'] = $tag->get_id_option(); | |
$atts = wpcf7_format_atts( $atts ); | |
$html = '<select ' . $atts . '>'; | |
$args = array( | |
'post_type' => 'retreats', | |
'posts_per_page' => -1, | |
); | |
$retreats = get_posts( $args ); | |
foreach ( $retreats as $retreat ): | |
$retreat_id = $retreat->ID; | |
$slug = $retreat->post_name; | |
$title = get_the_title($retreat_id); | |
$html .= '<option value="' . $slug . '">' . $title . '</option>'; | |
endforeach; | |
$html .= '</select>'; | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@morgyface
Thank you so much, I got a fix that works perfectly here: https://bdwm.be/how-to-create-dynamically-populated-cascading-dropdown-lists-for-contact-form-7/
I will however suggest, for future reference to anyone, to check the comments, there were some mistakes in the code that were corrected in the comment session.
Again, thanks for your help and the timely response.