I’d like to make my categories that have child-categories non-selectable on the post article page.
What I want to do is to remove the checkbox before their label.
I’ve looked at the filter documentation but I wasn’t able to find any filter that suited my need.
I really doubt this is filterable, so jQuery comes to rescue 🙂
The Code
The Result
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.
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).Almost exactly the same to brasofilo, my code below also accounts for different taxonomies and removes the checkbox for parent categories at the top level, only if they have children. This allows for other top level categories that are childless to still be selectable. I put this simple code my theme’s functions.php file:
The following solution prevents the checkbox being output. This seems to be much cleaner in my opinion.
I make a custom walker extending the core
Walker_Category_Checklist
which overrides thestart_el
function so it can conditionally set$args['list_only']
to true. In this case the function checks whether we have a top-level term ($category->parent == 0
) which has sub-terms ($args['has_children']
) and of course theres a check if we are using the right taxonomy. These conditions met,$args['list_only']
is set to true and the parent method is called, which will not output the checkbox for this element now.To make WordPress use this walker class instead of the original one we can use the filter
wp_terms_checklist_args
inwp_terms_checklist()
:This solution has the advantage of automatically working in all places which use the core function
wp_terms_checklist()
and it doesn’t depend on JavaScript.It can also be done with the ” Category Checklist Tree ” plugin to maintain the taxonomy hierarchy as brasofilo says and some CSS:
( #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:
And then add the stylesheet “admin.css to your theme directory and add the css above
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
I used a combination of the above to get my solution. I liked the pure css approach, however the label click action would still change the “checked” attribute of the checkbox even though it wasn’t showing. Here’s my solution, note that it is hard coded to hide the first two levels of categories.
css:
js (if the checkbox is hidden, cancel the click event. Will work at any level):
And to reference on admin: