Reputation: 2180
In the word press admin panel there is a category selection 'helper panel' which I am trying to recreate. However I can't find the code for it, could someone point me in the right direction please?
Upvotes: 0
Views: 185
Reputation: 11971
The actual Category Box that is created by Wordpress is not typically used by Plugins that utilize a Custom UI. However, you can mimic its behavior, and you were most certainly on the right track with get_categories(). If you want to grab ALL categories, not just the ones with a post count, you need to call it like so:
<?php
$args = array(
'type' => 'post',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0, //<--IMPORTANT!!
'hierarchical' => 1,
'taxonomy' => 'category',
'pad_counts' => false );
$categories = get_categories($args);
?>
'hide_empty' is what you were missing. Once you want to create your checkboxes, you would do something like this:
<form action="action.php" method="POST">
<?php
foreach($categories as $cat)
{
echo '<input type="checkbox" name="categories[]" value="'.$cat->cat_ID.'" />';
echo '<label>'$cat->name.'</label><br/>';
}
?>
<input type="text" name="user_input" value="" />
</form>
You can style the checkboxes however you like using a custom stylesheet, or you can apply the same tags and classes that the standard one uses, which will ensure that the existing Wordpress Admin Stylesheet styles everything accordingly.
Upvotes: 1