WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. cokacala
    Member
    Posted 11 months ago #

    Hi,

    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.

    http://wordpress.org/plugins/contact-form-7/

  2. cokacala
    Member
    Posted 11 months 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
    Member
    Posted 11 months ago #

    NICE!!!

  4. danielnichols
    Member
    Posted 11 months ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic