Skip to content

Instantly share code, notes, and snippets.

@mrfoxtalbot
Last active August 21, 2022 12:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrfoxtalbot/9fc41c709d05dd7aa1206ddaa55fd42a to your computer and use it in GitHub Desktop.
Save mrfoxtalbot/9fc41c709d05dd7aa1206ddaa55fd42a to your computer and use it in GitHub Desktop.
Auto-Populate Contact Form 7 field with post titles
<?php // Auto-Populate Contact Form
function mrfx_add_plugin_list_to_contact_form ( $tag, $unused ) {
if ( $tag['name'] != 'add-this-tag-to-CF7-field' )
return $tag;
$args = array ( 'post_type' => 'post-type-slug-goes-here',
'numberposts' => 99,
'orderby' => 'title',
'order' => 'ASC' );
$plugins = get_posts($args);
if ( ! $plugins )
return $tag;
foreach ( $plugins as $plugin ) {
$tag['raw_values'][] = $plugin->post_title;
$tag['values'][] = $plugin->post_title;
$tag['labels'][] = $plugin->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'mrfx_add_plugin_list_to_contact_form', 10, 2);
// Source: https://www.leewillis.co.uk/dynamic-select-list-contact-form-7/
//
// "add-this-tag-to-CF7-field" > Will be the field to be autopopulated
// "post-type-slug-goes-here" > Replace this slug with the post slug
// "'numberposts' => 99" > Number of posts to show in the dropdown
// 'orderby' => 'title', 'order' => 'ASC' > How to order and by which criteria
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment