Is there a plugin or some php code to do this:

I want to disable some categories from the category box on the add/edit message page. I don't want to delete the categories and I want to leave the disabled categories to the previously assigned posts.


I have solved the problem myself, but it is not a pretty one.

With the function attached to the add_meta_boxes action hook, I change the callback function that builds the category box. It replaces this function with the customized function post_categories_meta_box_custom.

post_categories_meta_box_custom is a copy of post_categories_meta_box from wp-admin/includes/meta-boxes.php. With the help of simple-html-dom the <li>...</li> with the specified id is removed.

function terms_meta_boxes($post_type, $post) {
    global $wp_meta_boxes;

    foreach ($wp_meta_boxes[$post_type] as $context => $priorities) {
        foreach ($priorities as $priority => $ids) {
            foreach ($ids as $id => $meta_box) {
                $tax = get_taxonomy( $meta_box['args']['taxonomy'] );
                if ( $tax->hierarchical ) {
                    $wp_meta_boxes[$post_type][$context][$priority][$id]['callback'] = 'post_categories_meta_box_custom';
add_action('add_meta_boxes', 'terms_meta_boxes', 10, 2);

 * Custom function to display post categories form fields.
 * Original wp function: post_categories_meta_box.
 * @param object $post
function post_categories_meta_box_custom( $post, $box ) {
    $defaults = array('taxonomy' => 'category');
    if ( !isset($box['args']) || !is_array($box['args']) )
        $args = array();
        $args = $box['args'];
    extract( wp_parse_args($args, $defaults), EXTR_SKIP );
    $tax = get_taxonomy($taxonomy);

    <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
        <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
            <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
            <li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>

        <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
            <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
                <?php $popular_ids = wp_popular_terms_checklist($taxonomy); ?>

        <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
            $name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
            echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
            <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
                wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids ) );
                $html_checklist = str_get_html(ob_get_clean());
                $disabled_cats = get_option('disabled_cats', array());
                foreach ($disabled_cats[$taxonomy] as $cat) {
                    $html_checklist->find('li#category-' . $cat, 0)->outertext = '';
                echo $html_checklist->save();
    <?php if ( current_user_can($tax->cap->edit_terms) ) : ?>
            <div id="<?php echo $taxonomy; ?>-adder" class="wp-hidden-children">
                    <a id="<?php echo $taxonomy; ?>-add-toggle" href="#<?php echo $taxonomy; ?>-add" class="hide-if-no-js" tabindex="3">
                            /* translators: %s: add new taxonomy label */
                            printf( __( '+ %s' ), $tax->labels->add_new_item );
                <p id="<?php echo $taxonomy; ?>-add" class="category-add wp-hidden-child">
                    <label class="screen-reader-text" for="new<?php echo $taxonomy; ?>"><?php echo $tax->labels->add_new_item; ?></label>
                    <input type="text" name="new<?php echo $taxonomy; ?>" id="new<?php echo $taxonomy; ?>" class="form-required form-input-tip" value="<?php echo esc_attr( $tax->labels->new_item_name ); ?>" tabindex="3" aria-required="true"/>
                    <label class="screen-reader-text" for="new<?php echo $taxonomy; ?>_parent">
                        <?php echo $tax->labels->parent_item_colon; ?>
                    <?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => 'new'.$taxonomy.'_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '&mdash; ' . $tax->labels->parent_item . ' &mdash;', 'tab_index' => 3 ) ); ?>
                    <input type="button" id="<?php echo $taxonomy; ?>-add-submit" class="add:<?php echo $taxonomy ?>checklist:<?php echo $taxonomy ?>-add button category-add-sumbit" value="<?php echo esc_attr( $tax->labels->add_new_item ); ?>" tabindex="3" />
                    <?php wp_nonce_field( 'add-'.$taxonomy, '_ajax_nonce-add-'.$taxonomy, false ); ?>
                    <span id="<?php echo $taxonomy; ?>-ajax-response"></span>
        <?php endif; ?>

Take a look at admin-only-categories plugin which allow you to hide certain category, chosen in the settings >> admin-only category settings panel, from all but Admin.

  • This plugin removes the whole category, but not specified items from a category. How can I do that? – user3499 Apr 11 '11 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy