• Resolved floi13

    (@floi13)


    Hi,

    I’ve got a Contact Form 7 form which users can use to make a bid on a product. Therese products are of custom post type ‘products’.
    Is it possible to create a radio button for each product cpt?

    At the moment I got
    [radio radio-products use_label_element default:1 "Product #1" "Product #2" "Product #3"], but I don’t want the products to be hard coded in there.
    Maybe something like
    [radio radio-products use_label_element default:1 data:cpt_name]?

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter floi13

    (@floi13)

    Solved the issue by doing the following things. Maybe this helps someone who tries a similar thing.

    1) add this code to my functions.php:

    function dynamic_select_field_values ( $scanned_tag, $replace ) {  
        if($scanned_tag['name'] != 'YOUR CF7 FIELDNAME HERE') return $scanned_tag;
        $rows = get_posts(
        	array ( 
    	     'post_type' => 'YOUR CPT NAME HERE',  
    	     'numberposts' => -1,  
    	     'orderby' => 'title',  
    	     'order' => 'ASC' 
            )
        );
        if(!$rows) return $scanned_tag;
        foreach($rows as $row) {  
            $scanned_tag['raw_values'][] = $row->post_title . '|' . $row->post_title;
        }
        $pipes = new WPCF7_Pipes($scanned_tag['raw_values']);
        $scanned_tag['values'] = $pipes->collect_befores();
        $scanned_tag['pipes'] = $pipes;
      
        return $scanned_tag;  
    }  
    add_filter( 'wpcf7_form_tag', 'dynamic_select_field_values', 10, 2);

    2) add an element with corresponding name in your CF7 form:
    [radio YOUR CF7 FIELDNAME HERE use_label_element default:1]

    Hello @floi13,

    The solution you applied is brilliant: thanks for sharing your code!

    It’s worth mentioning that it also works with select and checkbox fields. I tested it with some sample posts:

    Screenshot of a form of Contact Form 7 that display radio, select and checkbox options queried from the posts list

    Best regards,
    Yordan.

    Thread Starter floi13

    (@floi13)

    Hi Yordan,

    Ah, I forgot to mention that, thanks for doing so #thumbsup

    Hello from France,

    what is:
    type’ => ‘YOUR CPT NAME HERE’,

    Thanks
    Jacques

    Thread Starter floi13

    (@floi13)

    Hi @jackbluehouse2019

    You should replace YOUR CPT NAME HERE with the name of your custom post type.

    Hi flo13,
    your code works fine with posts. Now I need to do the same thing but with an array of items stored on db.
    Do you think it’s possible?
    Thanks

    Thread Starter floi13

    (@floi13)

    I’m not completely sure to be honest… give it a try, I would say.
    Worth mentioning is that in my case, it was purely based on a Contact Form 7 form, so didn’t do much if anything with database stuff.

    Ok I solved my problem by creating custom posts instead of db items.
    Therefor I can use your code to display dynamic content in dropdown menu.
    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Create radiobuttons with custom post type’ is closed to new replies.