Make WordPress Core

Changeset 37856


Ignore:
Timestamp:
06/23/2016 03:36:33 PM (8 years ago)
Author:
swissspidy
Message:

Posts list table: Add a filter to disable the categories dropdown.

This allows one to short-circuit its output before any queries are run, similar to how it was done for the months dropdown in [31438].

Props davidmosterd for initial patch.
Fixes #36152.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r37488 r37856  
    413413
    414414    /**
    415      * @global int $cat
     415     * Displays a categories drop-down for filtering on the Posts list table.
     416     *
     417     * @since 4.6.0
     418     * @access protected
     419     *
     420     * @global int $cat Currently selected category.
     421     *
     422     * @param string $post_type The Post Type.
     423     */
     424    protected function categories_dropdown( $post_type ) {
     425        global $cat;
     426
     427        /**
     428         * Filters whether to remove the 'Categories' drop-down from the post list table.
     429         *
     430         * @since 4.6.0
     431         *
     432         * @param bool   $disable   Whether to disable the categories drop-down. Default false.
     433         * @param string $post_type The post type.
     434         */
     435        if ( false !== apply_filters( 'disable_categories_dropdown', false, $post_type ) ) {
     436            return;
     437        }
     438
     439        if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
     440            $dropdown_options = array(
     441                'show_option_all' => get_taxonomy( 'category' )->labels->all_items,
     442                'hide_empty' => 0,
     443                'hierarchical' => 1,
     444                'show_count' => 0,
     445                'orderby' => 'name',
     446                'selected' => $cat
     447            );
     448
     449            echo '<label class="screen-reader-text" for="cat">' . __( 'Filter by category' ) . '</label>';
     450            wp_dropdown_categories( $dropdown_options );
     451        }
     452    }
     453
     454    /**
    416455     * @param string $which
    417456     */
    418457    protected function extra_tablenav( $which ) {
    419         global $cat;
    420458?>
    421459        <div class="alignleft actions">
     
    424462
    425463            $this->months_dropdown( $this->screen->post_type );
    426 
    427             if ( is_object_in_taxonomy( $this->screen->post_type, 'category' ) ) {
    428                 $dropdown_options = array(
    429                     'show_option_all' => get_taxonomy( 'category' )->labels->all_items,
    430                     'hide_empty' => 0,
    431                     'hierarchical' => 1,
    432                     'show_count' => 0,
    433                     'orderby' => 'name',
    434                     'selected' => $cat
    435                 );
    436 
    437                 echo '<label class="screen-reader-text" for="cat">' . __( 'Filter by category' ) . '</label>';
    438                 wp_dropdown_categories( $dropdown_options );
    439             }
     464            $this->categories_dropdown( $this->screen->post_type );
    440465
    441466            /**
Note: See TracChangeset for help on using the changeset viewer.