Question

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?

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top