Here is a ideal example of usage [Simple to understand/Easy to use]
-
Hey, guys!
This is one of the most awesome and important plugin extension for Contact Form 7 out there! It is simply very, very great and, if used correctly, can bring a lot of freedom on our contact forms! But… as some other people I noticed, I had a hard time to figure it out a correct and simple usage of the whole thing. So, I decided to help!
The scenario:
Let’s assume you want to add, on your contact form, a dynamic select/option presenting a choice of products, where your products are a custom post called “product” (I am using products just for example, it could be any custom post, or even “page” ou regular “post” as well).
STEP 1: On your functions.php
Add the following function/filter:
// Dynamic Select for Contact Form 7 function dynamic_select_for_custom_posts($choices, $args=array()) { // Here we grab the posts using the arguments originated from the shortcode $get_custom_posts = get_posts($args); // If we have posts, proceed if ($get_custom_posts) { // Foreach found custom post, we build the option using the [key] => [value] fashion foreach ($get_custom_posts as $custom_post) { $choices[$custom_post->post_title] = $custom_post->post_title; } // If we don't have posts, halt! Lets use a generic not found option } else { // Just a generic option to inform nothing was found $choices['No posts found'] = 'No posts found'; } return $choices; } // Lets add a suggestive name to our filter (we will use it on the shortcode) add_filter('conjure-posts-dynamically', 'dynamic_select_for_custom_posts', 10, 2);
As you can see above, inside the function we don’t mention any kind of argument related to custom posts, to post sorting, to number of posts and so on. We will provide this right away through the shortcode, written inside the CF7 form.
STEP 2: Inside the CF7 form:
To make use of the function created above, you will need to add the following shortcode to your CF7 form:
[dynamicselect* desired-product "conjure-posts-dynamically post_type=product posts_per_page=-1 orderby=title order=asc"]
It’s pretty simple! Our filter, created on STEP 1 has the name of “conjure-posts-dynamically”, so, we just call it inside the shortcode followed by traditional query_posts parameters as arguments.
Important: the filter name AND ITS arguments must be enclosed inside double quotes, all together.
I used the name “desired-product” in the select/option combo just to make things more easy to understand, but, of course, you should rename it to anything you want. Just to refresh the memory — the field name is the parameter that you set up inside the mail template, in the Email TAB (second tab) of the CF7 form builder.
Other examples using different arguments:
Regular post: Let’s grab only news categorized as videogame news limiting only to the last 10 entries:
[dynamicselect* desired-news "conjure-posts-dynamically post_type=post posts_per_page=10 orderby=date order=desc category=videogame"]
Taxonomy/term & Custom Post: Let’s grab only jazz records from our record collection:
[dynamicselect* desired-record "conjure-posts-dynamically post_type=record posts_per_page=-1 taxonomy=genre term=jazz"]
Meta Values & Custom Post: Let’s grab only Famicom games, from Nintendo:
[dynamicselect* desired-game "conjure-posts-dynamically post_type=game posts_per_page=-1 meta_key=nintendo meta_value=famicom"]
That’s it, guys.
I really hope to have been helpful.For the plugin author — THANK YOU SO MUCH for this plugin.
Feel free to add my post to the documentation if you find it out suitable.
- The topic ‘Here is a ideal example of usage [Simple to understand/Easy to use]’ is closed to new replies.