WordPress.org

Support

Support » Plugins and Hacks » Contact Form 7 » [Resolved] Generate dynamic checkboxes

[Resolved] Generate dynamic checkboxes

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    NICE!!!

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Generate dynamic checkboxes’ is closed to new replies.
Skip to toolbar