Quantcast
Channel: WordPress.org Forums » [Contact Form 7] Support
Viewing all articles
Browse latest Browse all 49500

cokacala on "[Plugin: Contact Form 7] Generate dynamic checkboxes"

$
0
0

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.


Viewing all articles
Browse latest Browse all 49500

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>