unregister_taxonomy

The timeline below displays how wordpress function unregister_taxonomy has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP_Taxonomy[] $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    global $wp_taxonomies;
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 6.1

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP            $wp            Current WordPress environment instance.
 * @global WP_Taxonomy[] $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 5.9

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global WP_Taxonomy[] $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove custom taxonomy default term option.
    if (!empty($taxonomy_object->default_term)) {
        delete_option('default_term_' . $taxonomy_object->name);
    }
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 5.7

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove custom taxonomy default term option.
    if (!empty($taxonomy_object->default_term)) {
        delete_option('default_term_' . $taxonomy_object->name);
    }
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 5.5

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return bool|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove custom taxonomy default term option.
    if (!empty($taxonomy_object->default_term)) {
        delete_option('default_term_' . $taxonomy_object->name);
    }
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 4.9

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return bool|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed.'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 4.7

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return bool|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_object = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_object->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed'));
    }
    global $wp_taxonomies;
    $taxonomy_object->remove_rewrite_rules();
    $taxonomy_object->remove_hooks();
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 4.6

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return bool|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $taxonomy_args = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_args->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed'));
    }
    global $wp, $wp_taxonomies;
    // Remove query var.
    if (false !== $taxonomy_args->query_var) {
        $wp->remove_query_var($taxonomy_args->query_var);
    }
    // Remove rewrite tags and permastructs.
    if (false !== $taxonomy_args->rewrite) {
        remove_rewrite_tag("%{$taxonomy}%");
        remove_permastruct($taxonomy);
    }
    // Unregister callback handling for meta box.
    remove_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term');
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}

WordPress Version: 4.5

/**
 * Unregisters a taxonomy.
 *
 * Can not be used to unregister built-in taxonomies.
 *
 * @since 4.5.0
 *
 * @global WP    $wp            Current WordPress environment instance.
 * @global array $wp_taxonomies List of taxonomies.
 *
 * @param string $taxonomy Taxonomy name.
 * @return bool|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 */
function unregister_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    }
    $taxonomy_args = get_taxonomy($taxonomy);
    // Do not allow unregistering internal taxonomies.
    if ($taxonomy_args->_builtin) {
        return new WP_Error('invalid_taxonomy', __('Unregistering a built-in taxonomy is not allowed'));
    }
    global $wp, $wp_taxonomies;
    // Remove query var.
    if (false !== $taxonomy_args->query_var) {
        $wp->remove_query_var($taxonomy_args->query_var);
    }
    // Remove rewrite tags and permastructs.
    if (false !== $taxonomy_args->rewrite) {
        remove_rewrite_tag("%{$taxonomy}%");
        remove_permastruct($taxonomy);
    }
    // Unregister callback handling for metabox.
    remove_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term');
    // Remove the taxonomy.
    unset($wp_taxonomies[$taxonomy]);
    /**
     * Fires after a taxonomy is unregistered.
     *
     * @since 4.5.0
     *
     * @param string $taxonomy Taxonomy name.
     */
    do_action('unregistered_taxonomy', $taxonomy);
    return true;
}