How to use wp_category_checklist()?

I’ve recently discovered that we can insert a dropdown list of categories like this:

define( 'WP_USE_THEMES', false );
require( './wp-load.php' );
wp_dropdown_categories( array(
    'child_of'         => 0,
    'class'            => 'postform', 
    'depth'            => 0,
    'echo'             => 1,
    'exclude'          => '', 
    'hide_empty'       => false, 
    'hide_if_empty'    => false,
    'hierarchical'     => true,
    'id'               => '',
    'name'             => 'cat-dropdown', 
    'order'            => 'ASC',
    'orderby'          => 'name', 
    'selected'         => 0, 
    'show_count'       => 0,
    'show_option_all'  => '', 
    'show_option_none' => __('None'),
    'tab_index'        => 0, 
    'taxonomy'         => 'format',
) );

This is great, BUT what if I want to select multiple categories?

Read More

I’ve come across wp_category_checklist() and tried this code:

function wp_category_checklist( 
    $post_id = 0, 
    $descendants_and_self = 0, 
    $selected_cats = false, 
    $popular_cats = false, 
    $walker = null, 
    $checked_ontop = true 
) {
    wp_terms_checklist( $post_id, array(
        'taxonomy'             => 'category',
        'descendants_and_self' => $descendants_and_self,
        'selected_cats'        => $selected_cats,
        'popular_cats'         => $popular_cats,
        'walker'               => $walker,
        'checked_ontop'        => $checked_ontop
    ) );
}

But it doesn’t output anything.

I’m trying to get a list of my categories on a BLANK PAGE, not within a sidebar or post. Is that possible?

Related posts

Leave a Reply

3 comments

  1. Here is the answer I ended up with:

    $select_cats = wp_dropdown_categories( array( 'echo' => 0 ) );
    $select_cats = str_replace( "name='cat' id=", "name='cat[]' multiple='multiple' id=", $select_cats );
    echo $select_cats;
    

    It displays a list of my categories that can be multi-selected as per my question. It’s a hack of wp_dropdown_categories, but it works.

  2. The function wp_terms_checklist is defined in the file /wp-admin/includes/template.php and therefore only available on the admin side of WordPress.

  3. If you want to select multiple custom taxonomy with checkbox. You can use that code..

     $html .= wp_dropdown_categories( array(
                            'taxonomy' => 'taxonomy_name',
                            'hierarchical' => 0,
    
                        ) );
    
    
    $html = str_replace( "form id=", "form multiple='multiple' id=", $html );
    $html = str_replace( "select", "span", $html );
    $html = str_replace( "option class=", "input type='checkbox' class=", $html );
    $html = str_replace( "option", "", $html );
    $html = str_replace( "</>", "<br/>", $html );
    
    echo $html;
    

    And Tf You want to select multiple categories with checkbox. You can use that code..

    $html = wp_dropdown_categories( array( 'echo' => 0, 'hierarchical' => 0 ) ); // Your Query here
    
    $html = str_replace( "form id=", "form multiple='multiple' id=", $html );
    $html = str_replace( "select", "span", $html );
    $html = str_replace( "option class=", "input type='checkbox' class=", $html );
    $html = str_replace( "option", "", $html );
    $html = str_replace( "</>", "<br/>", $html );
    
    echo $html;