How To Show Categories List With Checkbox By Using WordPress Walker Class

Hello Friends, When I was making my plugin IGIT Related posts then I needed a way to show all categories with checkbox So user can easily select categories by checkbox to exclude that categories from results. You can make custom function by fetching categories by queries or using any WordPress function to get categories but by using these ways I felt I will have some issues while having subcategories and after selection of categories we need to show categories again selected when user came back. So I thought to use WordPress core function and class to achieve that.

As we know WordPress easily implement this when you write post, it shows categories section at right side and you can easily select category or sub category and when you came back it automatically shows selected categories by passing array of selected categories.

Below is function to show categories with checkbox :

function wp_category_checklist_temp( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
if ( empty($walker) || !is_a($walker, 'Walker') )
$walker = new Walker_Category_Checklist;
$descendants_and_self = (int) $descendants_and_self;

$args = array();

if ( is_array( $selected_cats ) )
$args['selected_cats'] = $selected_cats;
elseif ( $post_id )
$args['selected_cats'] = wp_get_post_categories($post_id);
else
$args['selected_cats'] = array();

if ( is_array( $popular_cats ) )
$args['popular_cats'] = $popular_cats;
else
$args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );

if ( $descendants_and_self ) {
$categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0" );
$self = get_category( $descendants_and_self );
array_unshift( $categories, $self );
} else {
$categories = get_categories('get=all');
}

if ( $checked_ontop ) {
// Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
$checked_categories = array();
$keys = array_keys( $categories );

foreach( $keys as $k ) {
if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
$checked_categories[] = $categories[$k];
unset( $categories[$k] );
}
}

// Put checked cats on top
echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
}
// Then the rest of them
echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
}

You can call it like this :

wp_category_checklist_temp($post_ID, false,$exclude_cat_arr);

Remember you can change function name to anything but don’t name it ‘wp_category_checklist’ because as I pick this function from WordPress core and then make changes in it So ‘wp_category_checklist’ is WordPress core function and it will create issues if you name it ‘wp_category_checklist’.

When you call it you can see ‘$exclude_cat_arr’ it is an array of selected categories, So after save to database get selected categories and make array $exclude_cat_arr and pass it to this function it will show categories selected. $post_ID is post id.

I hope this code will help anyone. let me know if you need any help in this code.

I am PHP FreelancerHire WordPress Developer India and Hire WordPress Programmer. If you have any projects related to WordPress or PHP you can contact me.

7 Comments

  • DeNieD August 26, 2013 Reply

    I got Fatal error: Class ‘Walker_Category_Checklist’ not found. Why?

    • admin July 9, 2015 Reply Author

      Hi, Are you using above code in admin or front side? Walker_Category_Checklist is admin wordpress class (https://developer.wordpress.org/reference/classes/walker_category_checklist/) So if you are using above code in front end then you need to include file which is having this class like this :

      // This is required to be sure Walker_Category_Checklist class is available
      require_once ABSPATH . ‘wp-admin/includes/template.php’;

  • haberlerimiz September 2, 2014 Reply

    I got Fatal error: Class 'Walker_Category_Checklist' not found. Why?.

  • dsfgdfgfdg January 3, 2015 Reply

    I got Fatal error: Class ‘Walker_Category_Checklist’ not found. Why?.

  • Vogue Hairstyles February 9, 2015 Reply

    I got Fatal error: Class 'Walker_Category_Checklist' not found. Why?..

  • Herman May 10, 2015 Reply

    I got Fatal error: Class ‘Walker_Category_Checklist’ not found. Why?.

  • admin July 9, 2015 Reply Author

    For all who are getting : Fatal error: Class ‘Walker_Category_Checklist’ not found.

    Are you using above code in admin or front side? Walker_Category_Checklist is admin wordpress class (https://developer.wordpress.org/reference/classes/walker_category_checklist/) So it will automatically available in admin and if you are using above code in front end then you need to include file which is having class ‘Walker_Category_Checklist’ like this :

    // This is required to be sure Walker_Category_Checklist class is available
    require_once ABSPATH . ‘wp-admin/includes/template.php’;

Leave a Reply

Your email address will not be published. Required fields are marked *