Skip to content

Instantly share code, notes, and snippets.

@alkrauss48
Last active May 24, 2021 16:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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;
}
@alkrauss48
Copy link
Author

Call the shortcode like:

[postdropdown jobPosition post_type="job-opening"]

Where jobPosition is how you reference this field in the Contact Form 7 mailer, and job-opening is your post type

@govindak
Copy link

how to get category name on the option list category-title

@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