Last active
February 16, 2022 19:44
-
-
Save ajvillegas/adac4a847f723c5afe0a1c79fc56bd8a to your computer and use it in GitHub Desktop.
Remove Taxonomy Meta Box from Edit Screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function register_custom_taxonomy() { | |
$taxonomy_labels = [ | |
'name' => esc_html__( 'Custom Terms', 'text-domain' ), | |
'singular_name' => esc_html__( 'Custom Term', 'text-domain' ), | |
'menu_name' => esc_html__( 'Terms', 'text-domain' ), | |
]; | |
$taxonomy_args = [ | |
'labels' => $taxonomy_labels, | |
'rewrite' => [ | |
'slug' => 'custom-slug, | |
], | |
'show_in_rest' => true, | |
'show_ui' => true, | |
'show_in_quick_edit' => false, | |
'meta_box_cb' => false, | |
]; | |
register_taxonomy( 'custom_taxonomy_name', 'post_type_name', $taxonomy_args ); | |
} | |
add_action( 'init', 'register_custom_taxonomy' ); | |
/** | |
* Filters a taxonomy returned from the REST API. | |
* | |
* This function removes the taxonomy settings panel if the | |
* meta_box_cb arg is set to false when registering the taxonomy. | |
* | |
* @param WP_REST_Response $response The response object. | |
* @param WP_Taxonomy $taxonomy The original taxonomy object. | |
* @param WP_REST_Request $request Request used to generate the response. | |
* | |
* @return WP_REST_Response | |
*/ | |
function remove_taxonomy_settings_panel( $response, $taxonomy, $request ) { | |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | |
if ( $context === 'edit' && $taxonomy->meta_box_cb === false ) { | |
$data_response = $response->get_data(); | |
$data_response['visibility']['show_ui'] = false; | |
$response->set_data( $data_response ); | |
} | |
return $response; | |
} | |
add_filter( 'rest_prepare_taxonomy', 'remove_taxonomy_settings_panel', 10, 3 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment