On the Edit Screen in the WP Admin there is a meta box for WP’s built-in Category taxonomy. This meta box is built using post_categories_meta_box() (/wp-admin/includes/meta-boxes.php).
This meta box uses wp_popular_terms_checklist( $tax_name ) and wp_terms_checklist( $tax_name ) to output the actual categories (with checkboxes) within the meta box.
wp_terms_checklist() (wp-admin/includes/template.php) uses the Walker_Category_Checklist (/wp-admin/includes/class-walker-category.checklist) to build the categories/checkboxes.
Walker_Category_Checklist inherits from Walker (wp-includes/class-wp-walker.php) just like a number of other WP walkers (Walker_Nav_Menu, Walker_Comment, Walker_Category, etc.).
When extending the Walker_Nav_Menu we can hook into the wp_edit_nav_menu_walker filter and return our custom walker. Is there a way to do this with Walker_Category_Checklist?
===
Update 1:
I see in wp_terms_checklist() there is:
apply_filters( 'wp_terms_checklist_args', $args, $post_id );
I’m thinking that I can hook into this filter, change the walker argument to a custom walker and that this may do the trick?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes, as the documentation for wp_terms_checklist() stated, you can use the walker argument (which is part of the $args parameter for the wp_terms_checklist_args hook) to use a custom walker which modifies the output of the Category checklist generated by wp_terms_checklist(), e.g. the one in the “Categories” meta box on wp-admin/post.php.
Example where the custom walker is in a folder named includes:
add_filter( 'wp_terms_checklist_args', 'my_wp_terms_checklist_args', 10, 2 );
function my_wp_terms_checklist_args( $args, $post_id ) {
require_once __DIR__ . '/includes/class-my-walker-category-checklist.php';
$args['walker'] = new My_Walker_Category_Checklist;
return $args;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0