Support » Plugin: Dynamic Select for Contact Form 7 » 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.

    • This topic was modified 1 year, 9 months ago by  cospefogo. Reason: Typo
Viewing 5 replies - 1 through 5 (of 5 total)
  • I was actually going to do the same, cospefogo!
    Thank You so much and many thanks to John for the plugin.

    Thanks, Andrea!

    I forgot to mention that, for advanced users, it is possible
    to customize the function/filter to be suitable to any need.

    Even complex post queries or advanced stuff can be performed
    inside the function to achieve any special desired result.

    Regards,
    G.

    Plugin Author John Huebner

    (@hube2)

    @cospefogo

    Thank you for posting this and for the praise. I have seen the questions, but I just have not had the time to create an in depth explanation of how to use this plugin. I admit that the documentation only gives a very high level overview. Hopefully your explanation helps others as well.

    Thank you so much for this! I want to generate not a dropdown of posts but a dropdown (well how about a checkbox menu?) of terms of a particular custom taxonomy, and I want to preserve the hierarchy. Any clues on how to do that? I’m exploring for the solution myself and will post here as soon as I find a solution. Thanks again!

    I have come across a site having these subcategories being laid out as drop-down lists, is it possible to achieve this type of layout with Elementor? please refer the link:

    or can someone please point in the direction to achieve that effect
    thank you

    The page I need help with https://inkmesilly.com/printed-phone-cases/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Here is a ideal example of usage [Simple to understand/Easy to use]’ is closed to new replies.