Skip to content

Instantly share code, notes, and snippets.

@alkrauss48
Last active May 24, 2021 16:50
Show Gist options
  • Save alkrauss48/10680354 to your computer and use it in GitHub Desktop.
Save alkrauss48/10680354 to your computer and use it in GitHub Desktop.
Adding a custom Wordpress shortcode for building a dynamic dropdown of post type titles with Contact Form 7
wpcf7_add_shortcode('postdropdown', 'createbox', true);
function createbox(){
global $post;
extract( shortcode_atts( array( 'post_type' => 'some_post_type',), $atts ) );
$args = array('post_type' => $post_type );
$myposts = get_posts( $args );
$output = "<select name='postType' id='postType' onchange='document.getElementById(\"postType\").value=this.value;'><option></option>";
foreach ( $myposts as $post ) : setup_postdata($post);
$title = get_the_title();
$output .= "<option value='$title'> $title </option>";
endforeach;
$output .= "</select>";
return $output;
}
@justinasweb
Copy link

Hello,

How would I make this select field required? The standard way does not work: [postdropdown* jobPosition post_type="job-opening"]

@chrispink
Copy link

Unfortunately, possibly because of changes in CF7, this code no longer includes the output of the field in the mail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment