I am trying to change the checkboxes for the terms on the backend to radiobuttons.
I found this topic: Altering the appearance of custom taxonomy inputs however wich helped me doing this.
However, this will turn ALL terms checkboxes to radio buttons.
Is it possible to apply this only for one taxonomy?
My code:
add_action('add_meta_boxes','mysite_add_meta_boxes',10,2);
function mysite_add_meta_boxes($post_type, $post) {
ob_start();
}
add_action('dbx_post_sidebar','mysite_dbx_post_sidebar');
function mysite_dbx_post_sidebar() {
$html = ob_get_clean();
$html = str_replace('"checkbox"','"radio"',$html);
echo $html;
}
thanks
Not only that, it’ll turn any checkbox in a meta box – not ideal!
Instead, let’s specifically target the
wp_terms_checklist()
function, which is used to generate the list of checkboxes across the admin (including quick edit).We hook onto the
wp_terms_checklist_args
filter, then implement our own custom “walker” (a family of classes used to generate hierarchical lists). From there, it’s a simply string replace oftype="checkbox"
withtype="radio"
if the taxonomy is whatever we’ve configured it to match (in this case “category”).The following does pretty much what @TheDeadMedic did at his answer good answer, brought me there half the way, so this is kind of just an addition to it. Out of personal preference I opted to do it with
start_el
.→ make sure to replace YOUR-TAXONOMY in below code according to your needs
Now as @ Howdy_McGee correctly stated in his comment this doesn’t work nicely, correctly with the quick/inline edit. The above code handles the saving correctly, but the radio at the inline edit isn’t checked. Of course we want that, for this I have done this:
→ write some JQuery code to handle the checked state
→ file name: editphp-inline-edit-tax-radio-hack.js – used below for enqueueing
→ we need an AJAX action – as seen in above code block
→ enqueueing above script
→ change the path information according to your needs
This is working quite nicely so far, but only for the first time, when opening the inline edit a second time we have lost the checked state again. We obviously don’t want that. To get around it I used a method I’ve found here by @brasofilo. What it does is reloading the updated inline edit section. This leads to the radio checkbox being correctly shown, no matter how often it is changed.
→ make sure to replace YOUR-POST-TYPE in below code according to your needs
Note: Not extensively tested, but so far working well
You can use the
meta_box_cb
parameter of theregister_taxonomy
function to define your own function for themeta_box
. With the help of this article I have created this snippet:To make use of this meta_box, you have to pass this parameter to the
register_taxonomy
function:The beauty of this code is that you have to pass in no parameters at all, because it relies on the parameters passed to it by the
register_taxonomy
function. These are thepost
object and an array containing info on the metabox itself.If you would rather prefer to make it work with a plugin, you might want to take a look at https://wordpress.org/plugins/radio-buttons-for-taxonomies/
This plugin allows you to replace the default taxonomy boxes with a custom metabox that uses radio buttons… effectively limiting each post to a single term in that taxonomy.