Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save azizultex/f0b1aae10279c9a7233d to your computer and use it in GitHub Desktop.
Save azizultex/f0b1aae10279c9a7233d to your computer and use it in GitHub Desktop.
Getting All Contact Form 7 Forms in a Select Option and Showing Selected Form Anywhere Wordpress
// This is how I listed the forms in Piklist Select Option below:
$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1);
$cf7Forms = get_posts( $args );
// $post_ids = wp_list_pluck( $cf7Forms , 'ID' );
$form_titles = wp_list_pluck( $cf7Forms , 'post_title' );
piklist('field', array(
'type' => 'group'
,'field' => 'contact_form'
,'label' => __('Contact Form', 'worksfire')
,'columns' => 12
,'add_more' => false
,'fields' => array(
array(
'type' => 'text'
,'field' => 'title'
,'label' => __('Title', 'worksfire')
,'columns' => 3
)
,array(
'type' => 'textarea'
,'field' => 'description'
,'label' => __('description', 'worksfire')
,'columns' => 5
)
,array(
'type' => 'select'
,'field' => 'contact_form7'
,'value' => 'Select a Form' // Sets default to Option 2
,'label' => __('Select Form', 'worksfire')
,'columns' => 3
,'attributes' => array(
'class' => 'text'
)
,'choices' => $form_titles // All forms titles in an array
)
)
)
);
// Here is how I showed the selected form in a page
<div class="col-md-6">
<?php
$contact_title = parse_piklist_array(get_post_meta(get_the_ID(), 'contact_form', true));
foreach($contact_title as $cont) :
?>
<div class="headline"><h2><?php echo $cont['title']; ?></h2></div>
<!-- Begin Input Group -->
<p><?php echo $cont['description']; ?></p>
<div class="margin-bottom-20"></div>
<?php
$selectedFormkey = $cont['contact_form7']; // return a integer of the key
$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1);
$cf7Forms = get_posts( $args );
$form_ids = wp_list_pluck( $cf7Forms , 'ID' ); // Get the Forms ID in an Array
$getID = "";
foreach($form_ids as $key=>$value){
if( $key == $selectedFormkey ) { // this is where everything is happening to get the ID of the selected title compairing their key
$getID = $value;
}
}
echo do_shortcode('[contact-form-7 id="'.$getID.'"]');
?>
<?php endforeach; ?>
<!-- End Input-Group -->
</div>
@azizultex
Copy link
Author

Here is description:

Step 1 :

$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1); $cf7Forms = get_posts( $args );

Above codes will return all the contact for 7 forms

Step 2 :

$post_ids = wp_list_pluck( $cf7Forms , 'ID' ); $form_titles = wp_list_pluck( $cf7Forms , 'post_title' );

Above snippet will return form IDs and Titles in an array like below:

Step 3:

,'choices' => $form_titles

On line 36, this code will help getting the form titles in select option like below:

Step 4:

$selectedFormkey = $cont['contact_form7'];

will return a 0 or 1 from the screenshot above step 2

Step 5 :

$getID = ""; foreach($form_ids as $key=>$value){ if( $key == $selectedFormkey ) { $getID = $value; } }

On line 65, this is where everything is happening to get the ID of the selected title comparing their key like below:

Step 6:

echo do_shortcode('[contact-form-7 id="'.$getID.'"]');

will return the expected form

That's all :)

Here are some similar gists by me:

https://gist.github.com/azizultex/180b3d1c98d2dfea6424

https://gist.github.com/azizultex/614d294e3893e342538b

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