0

I'm the author of a plugin called Radio Buttons for taxonomies which converts certain taxonomy metaboxes (in classic) and sidebars (block editor) to use Radio buttons instead of checkboxes for selecting a term.

Unlike with checkboxes, which you can simply uncheck, with radio buttons if you want to offer someone the chance to "undo" a selection (or not apply a term) you need to provide an additional "term" they can select and then no save any value. It's a sort of pseudo term if you will.

Due to this, I filter the result of get_terms() and add a fake term with a 0 value. On save, I check for a 0 value for the term and make sure there are no terms saved for that post.

To reduce it to something y'all can test, here's an example code snippet:

/**
 * Add new 0 or null term in metabox and quickedit
 * this will allow users to "undo" a term if the taxonomy is not required
 *
 * @param array         $terms      Array of found terms.
 * @param array         $taxonomies An array of taxonomies.
 * @param array         $args       An array of get_terms() arguments.
 * @return array
 */
function kia_get_terms( $terms, $taxonomies, $args ) { 

    if ( isset( $args['fields'] ) && $args['fields'] == 'all' ) {

        $no_term = esc_html__( 'No term', 'my-text-domain' );

        $uncategorized = (object) array( 
            'term_id' => 0,
            'count' => 0,
            'description' => '',
            'name' => $no_term,
            'slug' => '0',
            'taxonomy' => 'category',
            'parent' => '0',
        );

        array_push( $terms, new WP_Term( $uncategorized ) );

    }

    return $terms;
}

add_filter( 'get_terms', 'kia_get_terms', 10, 3 );

In the classic editor, this works fine. But in the block editor (which still fetches the terms using get_terms() via the REST API... this causes an Uncaught (in promise) error and seems to get stuck in an infinite loop. How can I prevent this from looping?

error log showing recursion

7
  • have you considered instead replacing the panel with your own panel? I do not see the need for the pseudo-term in the block editor, it would be more effort to make it work that way than to simply eliminate it
    – Tom J Nowell
    Commented Oct 20, 2020 at 15:12
  • I do replace the panel in my plugin.. i replace it with a panel of radio buttons. But with radio buttons you can't uncheck an option so you need to provide an additional radio that will stand in for "no selection". My React skills are very dodgy, so sending the extra term back in the API response seemed like an easier approach to me. Commented Oct 20, 2020 at 16:24
  • You wouldn't unset the radio button, that markup and those tags are determined by the state, not the other way around, so just don't set a selected option in the react component. The tags themselves are disposable, so their current state is irrelevant
    – Tom J Nowell
    Commented Oct 20, 2020 at 17:26
  • I don't think I follow what you're saying. If you have radio buttons and you select "Tag A", but then later decide you want your post to have no tags, you have no way to clear that selection without a "no tag" radio button. You'd have to refresh the whole editor before save, or if you've saved there's no way to undo your selection. Commented Oct 20, 2020 at 18:44
  • 1
    TLDR: Add a hardcoded radio input named none, and when it's selected, clear the terms in the data store, fake terms are completely unnecessary
    – Tom J Nowell
    Commented Oct 21, 2020 at 10:34

1 Answer 1

1

The problem is you are passing "term_id" as 0 in your code. Use -1 instead!. It will definitely show up in the Block Editor. I guess this is a bug of Gutenberg though.

2
  • That did it!! Thank you! Could I ask where that is in the code? Or how you knew that? Now I just need to adjust my save routine to make sure it handles the -1 versus the old 0. Commented Oct 20, 2020 at 16:31
  • Kind of an add on question, but if you have any insight into how to show the new radio button as checked when there are no terms for a post, that's the next thing I'm working on. Commented Oct 20, 2020 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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