admin管理员组文章数量:1122846
I’d like to set a custom taxonomy as required, i.e. authors must select at least one term from the taxonomy in order to save or update a post of a specified set of custom post type. If they haven't done that, a custom error message displays.
I’m registering taxonomies using Extended CPTs, for example:
<?php
// Topic taxonomy
// Docs:
add_action('init', function () {
register_extended_taxonomy('topic', [
'article',
'event',
'resource',
], [
'exclusive' => false,
'hierarchical' => false,
'meta_box' => 'simple', // supports multiple (non-exclusive)
'public' => true, // applies to “publicly_queryable”, “show_ui”, and “show_in_nav_menus”
'query_var' => true,
'required' => true,
'show_admin_column' => true,
'show_in_quick_edit' => true,
'show_in_rest' => true,
], [
'singular' => 'Topic *',
'plural' => 'Topics',
'slug' => 'topic',
]);
});
How can this be accomplished (ideally, without using jQuery)?
I’d like to set a custom taxonomy as required, i.e. authors must select at least one term from the taxonomy in order to save or update a post of a specified set of custom post type. If they haven't done that, a custom error message displays.
I’m registering taxonomies using Extended CPTs, for example:
<?php
// Topic taxonomy
// Docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies
add_action('init', function () {
register_extended_taxonomy('topic', [
'article',
'event',
'resource',
], [
'exclusive' => false,
'hierarchical' => false,
'meta_box' => 'simple', // supports multiple (non-exclusive)
'public' => true, // applies to “publicly_queryable”, “show_ui”, and “show_in_nav_menus”
'query_var' => true,
'required' => true,
'show_admin_column' => true,
'show_in_quick_edit' => true,
'show_in_rest' => true,
], [
'singular' => 'Topic *',
'plural' => 'Topics',
'slug' => 'topic',
]);
});
How can this be accomplished (ideally, without using jQuery)?
Share Improve this question edited May 7, 2024 at 14:22 Chris_Heu asked May 7, 2024 at 14:09 Chris_HeuChris_Heu 12 bronze badges1 Answer
Reset to default 0You could consider using the wp_insert_post_empty_content
hook. This hook determines if a post is considered empty and if so, prevents it from being saved. For example:
/**
* Checks whether some post types have a topic.
*
* @param bool $maybe_empty Whether the post should be considered "empty".
* @param array $postarr Array of post data.
* @return bool Whether the post is empty.
*/
function my_plugin_require_topic( $maybe_empty, $postarr ) {
if ( ! $maybe_empty && in_array( $postarr['post_type'], array( 'article', 'event', 'resource' ), true ) {
$maybe_empty = empty( $postarr['tax_input']['topic'] );
}
return $maybe_empty;
}
add_filter( 'wp_insert_post_empty_content', 'my_plugin_require_topic', 10, 2 );
For how to implement the custom error message, that would depend on where you want the error message surfaced. This is because there are multiple ways that a post could be edited, and surfacing the error message would depend on the interface you'd want to target.
本文标签: How to enforce a requirement for custom taxonomy
版权声明:本文标题:How to enforce a requirement for custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307863a1933458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论