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?
- 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
1 Answer
Reset to default 1The 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
版权声明:本文标题:Add a term to list in block editor taxonomy sidebar 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047879a2417897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论