Skip to content

Instantly share code, notes, and snippets.

@PauliusKrutkis
Last active March 22, 2017 19:26
Show Gist options
  • Save PauliusKrutkis/dda783e6d269afeaccb3994f83537a27 to your computer and use it in GitHub Desktop.
Save PauliusKrutkis/dda783e6d269afeaccb3994f83537a27 to your computer and use it in GitHub Desktop.
Contact form 7 dynamic value populations
<?php
// contact form 7 dynamic value populations
// usage [select field-name values:post-list] (checkbox, radio works as well)
// for adding custom Form-Tag Check https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
function cf7DynamicListExample($tag)
{
$options = $tag['options'];
if (!isset($options))
return $tag;
foreach ($options as $option)
if (preg_match('%^values:([-0-9a-zA-Z_]+)$%', $option, $matches))
$arg = $matches[1];
if (!isset($arg))
return $tag;
switch ($arg) {
case 'post-list':
continue;
break;
default:
return $tag;
break;
}
$args = [
'post_type' => 'post',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC'
];
$posts = get_posts($args);
if (!$posts)
return $tag;
foreach ($posts as $post) {
$tag['raw_values'][] = $post->post_title;
$tag['values'][] = $post->post_title;
$tag['labels'][] = $post->post_title;
}
return $tag;
}
add_filter('wpcf7_form_tag', 'cf7DynamicListExample', 10, 2);
@viralmlive
Copy link

How can i show id slug value in input type id='value' (how to pass value in id)

@PauliusKrutkis
Copy link
Author

You can set the id for an input through the admin, but if you want to do it through code you can use the same hook - find the tag you're looking for and add:

$tag['options'][] = 'id:my-desired-id';

This will add an id "my-desired-id" to the input.

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