admin管理员组

文章数量:1318572

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?

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?

Share Improve this question asked Oct 20, 2020 at 14:55 helgathevikinghelgatheviking 14.5k8 gold badges64 silver badges115 bronze badges 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. – helgatheviking 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. – helgatheviking 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
 |  Show 2 more comments

1 Answer 1

Reset to default 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.

本文标签: Add a term to list in block editor taxonomy sidebar