Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like that my categories having child-categories don't be selectable on the post article page.

What I want to do is to remove the checkbox before their label.

I've looked the filter documentation but I wasn't able to find any filter that suits my need.

share|improve this question
    
What do you mean selectable? Can you explain please? –  Jared Feb 24 '12 at 1:56
    
You can still check the parent categories to use them instead of their children. –  koskoz Feb 24 '12 at 17:09

3 Answers 3

up vote 1 down vote accepted

I really doubt this is filterable, so jQuery comes to rescue :)

The Code

add_action( 'admin_footer-post.php', 'wpse_22836_remove_top_categories_checkbox' );
add_action( 'admin_footer-post-new.php', 'wpse_22836_remove_top_categories_checkbox' );

function wpse_22836_remove_top_categories_checkbox()
{
    global $post_type;

    if ( 'post' != $post_type )
        return;
    ?>
        <script type="text/javascript">
            jQuery("#categorychecklist>li>label input").each(function(){
                jQuery(this).remove();
            });
        </script>
    <?php
}

The Result

no parent categories


Advanced

There's a caveat: whenever selecting a sub-category, it gets out of the hierarchy...

So, the following is the code from the excellent plugin by Scribu, Category Checklist Tree, coupled with the previous code.

On the post editing screen, after saving a post, you will notice that the checked categories are displayed on top, breaking the category hierarchy. This plugin removes that "feature".

Either you use the previous code and install the plugin, or simply drop this in your theme's functions.php or in a custom plugin of yours (preferable, so all your tweaks will be theme independent).

/*
Based on Category Checklist Tree, by scribu
Preserves the category hierarchy on the post editing screen
Removes parent categories checkbox selection
*/
class Category_Checklist {

    function init() {
        add_filter( 'wp_terms_checklist_args', array( __CLASS__, 'checklist_args' ) );
    }

    function checklist_args( $args ) {
        add_action( 'admin_footer', array( __CLASS__, 'script' ) );

        $args['checked_ontop'] = false;

        return $args;
    }

    // Scrolls to first checked category
    function script() {
?>
<script type="text/javascript">
    jQuery(function(){
        jQuery('[id$="-all"] > ul.categorychecklist').each(function() {
            var $list = jQuery(this);
            var $firstChecked = $list.find(':checked').first();

            if ( !$firstChecked.length )
                return;

            var pos_first = $list.find(':checkbox').position().top;
            var pos_checked = $firstChecked.position().top;

            $list.closest('.tabs-panel').scrollTop(pos_checked - pos_first + 5);
        });

        jQuery("#categorychecklist>li>label input").each(function(){
            jQuery(this).remove();
        });

    });
</script>
<?php
    }
}

Category_Checklist::init();
share|improve this answer
    
Probably the Most Used tab needs to be removed as well..: jQuery('#category-tabs .hide-if-no-js').remove(); –  brasofilo Jul 14 '12 at 21:31

It can also be done with the " Category Checklist Tree " plugin to maintain the taxonomy hierarchy as brasofilo says and some CSS:

#categorychecklist > li > label.selectit > input { display: none !important; }

( #categorychecklist can be replaced with #yourcustomtaxonomychecklist to apply this to a custo taxonomy )

To add this, and other css to wordpress admin the following can be added to functions.php:

function my_admin_head() {
    echo '<link rel="stylesheet" type="text/css"
    href="'.get_bloginfo('stylesheet_directory').'/admin.css">';
}
add_action('admin_head', 'my_admin_head');

And then add the stylesheet "admin.css to your theme directory and add the css above

share|improve this answer
    
You should be enqueueing the stylesheet not echoing it directly into the head. –  s_ha_dum Feb 23 '13 at 16:32

Here's what I used to disable the parent categories and only allow one category per post. This code changes the quick edit categories, too. I welcome someone to clean it up, I'm not confident in javascript.

I got the first part here: https://wordpress.org/support/topic/making-category-selection-radio-buttons

    /* ONLY ALLOW ONE CATEGORY PER POST */
add_action( 'admin_footer', 'catlist2radio' );
function catlist2radio(){
    echo '<script type="text/javascript">';
    echo 'jQuery("#categorychecklist .children input, #categorychecklist-pop .children input, .cat-checklist .children input")';
    echo '.each(function(){this.type="radio"});</script>';
}

/* DISABLE PARENT CATEGORIES FOR POSTS */
/*
Based on Category Checklist Tree, by scribu
Preserves the category hierarchy on the post editing screen
Removes parent categories checkbox selection
*/
class Category_Checklist {
    function init() {
        add_filter( 'wp_terms_checklist_args', array( __CLASS__, 'checklist_args' ) );
    }
    function checklist_args( $args ) {
        add_action( 'admin_footer', array( __CLASS__, 'script' ) );
        $args['checked_ontop'] = false;
        return $args;
    }

    // Scrolls to first checked category
    function script() {
?>
<script type="text/javascript">
    jQuery(function(){
        jQuery('[id$="-all"] > ul.categorychecklist').each(function() {
            var $list = jQuery(this);
            var $firstChecked = $list.find(':checked').first();
            if ( !$firstChecked.length )
                return;
            var pos_first = $list.find(':checkbox').position().top;
            var pos_checked = $firstChecked.position().top;
            $list.closest('.tabs-panel').scrollTop(pos_checked - pos_first + 5);
        });
        jQuery("#categorychecklist>li>label input").each(function(){
            jQuery(this).remove();
        });
    });
        jQuery(function(){
        jQuery('[id$="-all"] > ul.cat-checklist').each(function() {
            var $list = jQuery(this);
            var $firstChecked = $list.find(':checked').first();
            if ( !$firstChecked.length )
                return;
            var pos_first = $list.find(':checkbox').position().top;
            var pos_checked = $firstChecked.position().top;
            $list.closest('.tabs-panel').scrollTop(pos_checked - pos_first + 5);
        });
        jQuery(".cat-checklist>li>label input").each(function(){
            jQuery(this).remove();
        });
    });
        jQuery('#category-tabs .hide-if-no-js').remove();
</script>
<?php
    }
}
Category_Checklist::init();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.