skip to Main Content

I want to add a new custom filter in WordPress network admin

restrict_manage_users for network admin in the user panelenter image description here

I want a custom filter option not a custom column, i just want to add one filter dropdown with roles

2

Answers


  1. add_filter( 'wpmu_users_columns', 'my_awesome_new_column' );
    
    add_action( 'manage_users_custom_column', 'my_awesome_column_data', 10, 3 );
    
    // Creates a new column in the network users table and puts it before a chosen column
    function my_awesome_new_column( $columns ) {
        return my_awesome_add_element_to_array( $columns, 'my-awesome-column', 'Awesome', 'registered' );
    }
    
    // Adds data to our new column
    function my_awesome_column_data( $value, $column_name, $user_id ) {
    
        // If this our column, we return our data
        if ( 'my-awesome-column' == $column_name ) {
            return 'Awesome user ID ' . intval( $user_id );
        }
    
        // If this is not any of our custom columns we just return the normal data
        return $value;
    }
    
    // Adds a new element in an array on the exact place we want (if possible).
    function my_awesome_add_element_to_array( $original_array, $add_element_key, $add_element_value, $add_before_key ) {
    
        // This variable shows if we were able to add the element where we wanted
        $added = 0;
    
        // This will be the new array, it will include our element placed where we want
        $new_array = array();
    
        // We go through all the current elements and we add our new element on the place we want
        foreach( $original_array as $key => $value ) {
    
            // We put the element before the key we want
            if ( $key == $add_before_key ) {
                $new_array[ $add_element_key ] = $add_element_value;
    
                // We were able to add the element where we wanted so no need to add it again later
                $added = 1;
            }
    
            // All the normal elements remain and are added to the new array we made
            $new_array[ $key ] = $value;
        }
    
        // If we failed to add the element earlier (because the key we tried to add it in front of is gone) we add it now to the end
        if ( 0 == $added ) {
            $new_array[ $add_element_key ] = $add_element_value;
        }
    
        // We return the new array we made
        return $new_array;
    }
    

    Here is an example : https://wordpress.stackexchange.com/questions/299801/custom-column-under-all-users-multisite-network-admin

    Login or Signup to reply.
  2. You will need to hook on the ‘restrict_manage_posts‘ filters to add your filter (dropdown) and to ‘parse_query‘ to alter the query according to the filter selection.
    For example, if you want to add a filter to display a list of years for the user to select, add the following code in your functions.php file:

    if (is_admin()){
       //this hook will create a new filter on the admin area for the specified post type
       add_action( 'restrict_manage_posts', function(){
            global $wpdb, $table_prefix;
    
            $post_type = (isset($_GET['post_type'])) ? quote_smart($_GET['post_type'], true) : 'post';
     
            //only add filter to post type you want
            if ($post_type == 'YOUR_POST_TYPE_HERE'){
                //query database to get a list of years for the specific post type:
                $values = array();
                $query_years = $wpdb->get_results("SELECT year(post_date) as year from ".$table_prefix."posts
                        where post_type='".$post_type."'
                        group by year(post_date)
                        order by post_date");
                foreach ($query_years as &$data){
                    $values[$data->year] = $data->year;
                }
    
                //give a unique name in the select field
                ?><select name="admin_filter_year">
    <option value="">All years</option>
    
                    <?php 
                    $current_v = isset($_GET['admin_filter_year'])? $_GET['admin_filter_year'] : '';
                    foreach ($values as $label => $value) {
                        printf(
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
                    ?>
                </select>
                <?php
            }
        });
    
        //this hook will alter the main query according to the user's selection of the custom filter we created above:
        add_filter( 'parse_query', function($query){
            global $pagenow;
            $post_type = (isset($_GET['post_type'])) ? quote_smart($_GET['post_type'], true) : 'post';
    
            if ($post_type == 'YOUR_POST_TYPE_HERE' && $pagenow=='edit.php' && isset($_GET['admin_filter_year']) && !empty($_GET['admin_filter_year'])) {
                $query->query_vars['year'] = $_GET['admin_filter_year'];
            }
        });
    }
    

    Using this technique you can actually add any filter you want.

    I hope this will solve your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search