Contact Form 7
[resolved] Generate dynamic checkboxes (4 posts)

  1. cokacala
    Posted 2 years ago #


    I have to display checkboxes for every parents categories that I have in my wordpress on my contact form, I wondered if there were any way to do it with cf7 ? I tried to type php inside, but it only takes shortcodes.

    Thank you.

    PS: Sorry for my english, i'm french.


  2. cokacala
    Posted 2 years ago #

    Ok i found how to do this, for those who need:

    In functions.php:

    function wpcf7_parentcats_shortcode_handler($tag){
        global $wpcf7_contact_form;
        $posted = wpcf7_is_posted();
        global $wpdb;
        $q = "SELECT term_id as id, name, slug FROM wp_terms where term_id in (SELECT term_id FROM wp_term_taxonomy where parent = 0 and taxonomy = 'category') order by name asc";
        $rows = $wpdb->get_results($q);
        $rows_length = count($rows);
        $return ='';
        for($i = 0; $i < $rows_length; $i++) {
        	if($i == 0) {
    	    	$return .= '<div class = "span3">';
            $return .= '<span class="wpcf7-list-item">';
            $return .= '<input type="checkbox" value="'.$rows[$i]->name.'" name=$rows[$i]->name>';
            $return .= ' <span class="wpcf7-list-item-label">' . esc_html( $rows[$i]->name ). '</span>';
            $return .= '</span>';
        	if(($i+1) % ceil($rows_length / 4) == 0){
    	    	$return .= '</div><div class = "span3">';
            if ($i == $rows_length - 1) {
    	        $return .= '</div>';
        return $return;

    I wanted to align them in 4 columns so i used twitter bootstrap, that's why i use a for loop and not a foreach.

    Then, still in functions.php:

    wpcf7_add_shortcode( 'checkboxparentcats', 'wpcf7_parentcats_shortcode_handler', true );

    Then you just have to use the shortcode [checkboxparentcats parentcats] in your form.

    Hope this helps.

  3. bernardberry646
    Posted 2 years ago #


  4. danielnichols
    Posted 2 years ago #

    what exactly does this code do please - like the idea of a dynamic form.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic