Make WordPress Core

Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#6916 closed enhancement (fixed)

wp_category_checklist() should take Walker object as argument

Reported by: whoismanu Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords: walker wp_category_checklist
Focuses: Cc:


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));

Change History (4)

#1 @ryan
10 years ago

  • Milestone changed from 2.7 to 2.8

Agreed, but moving to 2.8 since there is no patch yet.

9 years ago

  • Milestone changed from 2.8 to Future Release

#3 @whoismanu
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

Looks like this one got fixed in the meantime: [10246]

#4 @Denis-de-Bernardy
9 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.