Make WordPress Core


Ignore:
Timestamp:
01/09/2016 02:57:22 PM (8 years ago)
Author:
swissspidy
Message:

Taxonomy: Introduce unregister_taxonomy().

This new function can be used to completely unregister non built-in taxonomies.

Fixes #35227.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r36214 r36243  
    474474     */
    475475    do_action( 'registered_taxonomy', $taxonomy, $object_type, $args );
     476}
     477
     478/**
     479 * Unregister a taxonomy.
     480 *
     481 * Can not be used to unregister built-in taxonomies.
     482 *
     483 * @since 4.5.0
     484 *
     485 * @global WP    $wp            Current WordPress environment instance.
     486 * @global array $wp_taxonomies List of taxonomies.
     487 *
     488 * @param string $taxonomy Taxonomy name.
     489 * @return bool|WP_Error True on success, WP_Error on failure.
     490 */
     491function unregister_taxonomy( $taxonomy ) {
     492    if ( ! taxonomy_exists( $taxonomy ) ) {
     493        return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     494    }
     495
     496    $taxonomy_args = get_taxonomy( $taxonomy );
     497
     498    // Do not allow unregistering internal taxonomies.
     499    if ( $taxonomy_args->_builtin ) {
     500        return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed' ) );
     501    }
     502
     503    global $wp, $wp_taxonomies;
     504
     505    // Remove query var.
     506    if ( false !== $taxonomy_args->query_var ) {
     507        $wp->remove_query_var( $taxonomy_args->query_var );
     508    }
     509
     510    // Remove rewrite tags and permastructs.
     511    if ( false !== $taxonomy_args->rewrite ) {
     512        remove_rewrite_tag( "%$taxonomy%" );
     513        remove_permastruct( $taxonomy );
     514    }
     515
     516    // Unregister callback handling for metabox.
     517    remove_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
     518
     519    // Remove the taxonomy.
     520    unset( $wp_taxonomies[ $taxonomy ] );
     521
     522    /**
     523     * Fires after a taxonomy is unregistered.
     524     *
     525     * @since 4.5.0
     526     *
     527     * @param string $taxonomy Taxonomy name.
     528     */
     529    do_action( 'unregistered_taxonomy', $taxonomy );
     530
     531    return true;
    476532}
    477533
Note: See TracChangeset for help on using the changeset viewer.