logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

This is an old version of this answer!

Return to the current answer
I do this by writing a custom walker so I can change the name attribute to match my form. (At least until bug #16437 gets fixed.)

In the options form:


<ul class="categorychecklist">
<?php wp_category_checklist(0, 0, $options['categories'], false, new My_Walker_Category_Checklist, false); ?>
</ul>


And somewhere outside the form:


// custom walker so we can change the name attribute of the category checkboxes
// mostly a duplicate of Walker_Category_Checklist
class My_Walker_Category_Checklist extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
}

function end_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}

function start_el(&$output, $category, $depth, $args) {
extract($args);
if ( empty($taxonomy) )
$taxonomy = 'category';

// This is the part that's changed from the original walker
$name = 'check_categories[categories]';

$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
}

function end_el(&$output, $category, $depth, $args) {
$output .= "</li>\n";
}
}


... and in your plugin's CSS, this will make the checkbox list look like the one on the Edit screens:


ul.categorychecklist { height: 15em; width: 20em; overflow-y: scroll; border: 1px solid #dfdfdf; padding: 0 1em; background: #fff; border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px; }
ul.categorychecklist ul.children { margin-left: 1em; }

Stephanie Leary | 07/28/12 at 10:06am

This is an old version of this answer!

Return to the current answer