admin管理员组文章数量:1122832
I'm working on creating a plugin for WordPress and I've encountered an issue with some of my post types that have custom metaboxes for categorization. However, the default metaboxes are still being displayed. This results in the category selection appearing twice, which is unnecessary.
I started looking for a solution to remove the default metabox but I'm having trouble figuring it out.
public function register_taxonomy() {
$args = array(
'labels' => $this->taxLabel,
'public' => true,
'hierarchical' => true,
'show_ui' => false,
'show_in_rest' => true,
);
register_taxonomy($this->typeTax, $this->slug, $args);
$location_args = array(
'labels' => $this->locationLabel,
'public' => true,
'hierarchical' => true,
'show_ui' => false,
'show_in_rest' => true,
);
register_taxonomy($this->locationTax, $this->slug, $location_args);
}
When I use 'show_ui' => false in the script, I achieve my goal of only having my custom metabox in the post editor. However, this also means I lose the ability to manage categories in WordPress.
add_action('add_meta_boxes', array($this, 'remove_default_category_metabox'), 11);
public function remove_default_category_metabox() {
// Remove the default category taxonomy metabox
remove_meta_box('categorydiv', $this->slug, 'side'); // For default categories
remove_meta_box('tagsdiv-news_category', $this->slug, 'side'); // For non-hierarchical custom taxonomies
remove_meta_box('news_categorydiv', $this->slug, 'side'); // For hierarchical custom taxonomies
}
I've called add_action after my taxonomy has been registered. I've found remove_default_category_metabox a few times online, but it doesn't seem to work at all.
Can anyone help me with this? I hope you understand my problem and what I'm trying to achieve.
Thank you in advance.
I'm working on creating a plugin for WordPress and I've encountered an issue with some of my post types that have custom metaboxes for categorization. However, the default metaboxes are still being displayed. This results in the category selection appearing twice, which is unnecessary.
I started looking for a solution to remove the default metabox but I'm having trouble figuring it out.
public function register_taxonomy() {
$args = array(
'labels' => $this->taxLabel,
'public' => true,
'hierarchical' => true,
'show_ui' => false,
'show_in_rest' => true,
);
register_taxonomy($this->typeTax, $this->slug, $args);
$location_args = array(
'labels' => $this->locationLabel,
'public' => true,
'hierarchical' => true,
'show_ui' => false,
'show_in_rest' => true,
);
register_taxonomy($this->locationTax, $this->slug, $location_args);
}
When I use 'show_ui' => false in the script, I achieve my goal of only having my custom metabox in the post editor. However, this also means I lose the ability to manage categories in WordPress.
add_action('add_meta_boxes', array($this, 'remove_default_category_metabox'), 11);
public function remove_default_category_metabox() {
// Remove the default category taxonomy metabox
remove_meta_box('categorydiv', $this->slug, 'side'); // For default categories
remove_meta_box('tagsdiv-news_category', $this->slug, 'side'); // For non-hierarchical custom taxonomies
remove_meta_box('news_categorydiv', $this->slug, 'side'); // For hierarchical custom taxonomies
}
I've called add_action after my taxonomy has been registered. I've found remove_default_category_metabox a few times online, but it doesn't seem to work at all.
Can anyone help me with this? I hope you understand my problem and what I'm trying to achieve.
Thank you in advance.
Share Improve this question asked Aug 19, 2024 at 11:06 mikonorumikonoru 111 bronze badge 1- I recall searching for a way to remove default category metaboxes correctly to no avail as well. That was a few years ago. When my research time was spent and the client needed solutions, I settled for highly selective CSS and jQuery. – James Valeii Commented Aug 19, 2024 at 12:00
2 Answers
Reset to default 0I have approached this situation by using jQuery and CSS (this is code from a project that's been in production for a few years that I generalized):
// Admin Styles
function style_custom_taxonomy_admin() {
if (is_admin() && $_GET['taxonomy'] === 'TAXONOMY') {
add_action('admin_footer', function() {
echo '<style>/* Custom admin styles */</style>';
echo '<script>jQuery(document).ready(function($){
$("h2:contains(\"Heading to be hidden\")").length.remove();
$(".form_element_to_remove").remove();
</script>
});
}
}
add_action('init', 'style_custom_taxonomy_admin');
If you do not need the default taxonomies, you can create your own to start off with a simpler interface.
// Register Custom Taxonomy
function THIS_custom_taxonomy() {
register_taxonomy('TAXONOMY', ['post_type'], [
'labels' => [
'name' => 'TAXONOMY',
'singular_name' => 'TAXONOMY',
'menu_name' => 'TAXONOMY',
],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
]);
}
add_action('init', 'THIS_custom_taxonomy', 0);
Then you can add Taxonomy Meta (which you can also do with default taxonomies, of course):
// Edit Custom Taxonomy Meta
function add_custom_taxonomy_meta($term) {
$term_meta = get_term_meta($term->term_id);
?>
<h3>Custom Fields</h3>
<input type="text" name="term_meta[field1]" value="<?= $term_meta['field1'][0] ?? '' ?>">
<input type="text" name="term_meta[field2]" value="<?= $term_meta['field2'][0] ?? '' ?>">
<?php
}
add_action('TAXONOMY_edit_form_fields', 'add_custom_taxonomy_meta');
{$taxonomy}_edit_form_fields
And save the custom meta fields:
// Save Custom Taxonomy Meta
function save_custom_taxonomy_meta($term_id) {
if (!isset($_POST['term_meta'])) return;
foreach ($_POST['term_meta'] as $key => $value) {
update_term_meta($term_id, $key, $value);
}
}
add_action('edited_TAXONOMY', 'save_custom_taxonomy_meta', 10, 2);
edited_{$taxonomy}
If you are using the Classic Editor for your post types, the code you were using won't work because you are not targeting the correct metabox IDs.
Or least from what I can tell, since in the register_taxonomy_function you are using $this->typeTax and $this->locationTax but in your remove_meta_box you are targeting "category", "tags" and "news_category".
This modified code should work:
add_action( 'add_meta_boxes', [ $this, 'remove_default_category_metabox' ], 11);
public function remove_default_category_metabox() {
remove_meta_box( $this->typeTax . 'div', 'post', 'side' );
remove_meta_box( $this->locationTax . 'div', 'post', 'side' );
}
And, if you aren't using the Classic Editor, remove_meta_box won't work when you have show_in_rest set to true because it won't be added as a standard metabox in Gutenberg.
If you are using Gutenberg this is the code you will need:
add_filter( 'rest_prepare_taxonomy', [ $this, 'on_rest_prepare_taxonomy' ], 10, 3 );
function on_rest_prepare_taxonomy( $response, $taxonomy, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
if ( $context === 'edit' && ( $taxonomy->name === $this->typeTax || $taxonomy->name === $this->locationTax ) ) {
$data_response = $response->get_data();
$data_response['visibility']['show_ui'] = false;
$response->set_data( $data_response );
}
return $response;
}
本文标签: pluginsHow to Remove Default Category Metabox in Custom Post Types
版权声明:本文标题:plugins - How to Remove Default Category Metabox in Custom Post Types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297578a1930040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论