#6916 closed enhancement (fixed)
wp_category_checklist() should take Walker object as argument
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | General | Keywords: | walker wp_category_checklist |
Focuses: | Cc: |
Description
currently, the beginning of the wp_category_checklist() function in template.php looks like this
function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false ) { $walker = new Walker_Category_Checklist;
i.e. the Walker object is constructed inside the function. Now, the Walker object follows a visitor pattern which is nice, but creating it like this inside the function in my opinion takes away many of its benefits. I would rather see the Walker object passed in as an argument so that e.g. plugin authors can define their own custom Walkers. This would be much cleaner and versatile.
Example: My plugin needs to edit several posts on the same page. so for the categories i would like to have the checkboxes in array form, something like this
<input value="' . $category->term_id . '" type="checkbox" name="post_category['.$post_id.'][]"/>
now the current Walker_Category_Checklist defined in template.php outputs the category list using
<input value="' . $category->term_id . '" type="checkbox" name="post_category[]"/>
so assuming that i defined my own Walker (e.g. MyWalker) that outputs the categories as desired, the only thing i can do is to duplicate the whole wp_category_checklist() function and replace the first line of the function with something like
$walker = new MyWalker($post_id);
If the wp_category_checklist() function would fully adhere to the visitor pattern and take the visitor object (the Walker) as an argument as suggested here, the whole thing would be much cleaner and easier, i could just call the existing wp_category_checklist() function like this
wp_category_checklist(new MyWalker($post_id));
Agreed, but moving to 2.8 since there is no patch yet.