wordpress wordprees多站点在用户面板

yhuiod9q  于 2023-01-04  发布在  WordPress
关注(0)|答案(2)|浏览(103)

我想在WordPress网络管理中添加一个新的自定义过滤器
用户面板中网络管理员的restrict_manage_users

我想要一个自定义过滤器选项,而不是一个自定义列,我只想添加一个过滤器下拉列表与角色

5m1hhzi4

5m1hhzi41#

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;
}

下面是一个例子:https://wordpress.stackexchange.com/questions/299801/custom-column-under-all-users-multisite-network-admin

rt4zxlrg

rt4zxlrg2#

您需要在'restrict_manage_posts'过滤器上挂钩来添加您的过滤器(下拉列表),并在'parse_query'中挂钩来根据过滤器选择更改查询。例如,如果您想添加一个过滤器来显示一个年份列表供用户选择,请在functions.php文件中添加以下代码:
if(is_admin()){ //此钩子将在管理区域上为指定的发布类型add_action('restrict_manage_posts',function(){ global $wpdb,$table_prefix;
所有年份
}
使用此技术,您实际上可以添加任何所需的过滤器。
我希望这能解决你的问题。

相关问题