[wp-hackers] Multisite global user setting reset

Nikola Nikolov nikolov.tmw at gmail.com
Mon Sep 30 20:50:34 UTC 2013


The quickest thing I can think of is a mu-plugin, which would hook to one
of the query filters and would simply override the posts_per_page(in case
of users it would be different) query variable.

Here is an example plugin that you can place straight into *
/wp-content/mu-plugins/override-counts.php* :


<?php
/*
Plugin Name: Override WP List Table limit preferences
*/


// Override the posts per page variable
function my_ppp_query_overload( $vars ) {
    if ( is_admin() ) { // Only affect the admin queries
        $vars['posts_per_page'] = *20*;
    }
    return $vars;
}
add_filter( 'request', 'my_ppp_query_overload' );

function my_users_number_query_overload( $query ) {
	if ( is_admin() ) { // Only affect the admin queries
		global $wpdb;
		$query->query_vars['number'] = *20*;
		// limit
		if ( isset( $query->query_vars['number'] ) && $query->query_vars['number'] ) {
			if ( $query->query_vars['offset'] ) {
				$query->query_limit = $wpdb->prepare("LIMIT %d, %d",
$query->query_vars['offset'], $query->query_vars['number']);
			} else {
				$query->query_limit = $wpdb->prepare("LIMIT %d",
$query->query_vars['number']);
			}
		}
	}
}
add_action( 'pre_user_query', 'my_users_number_query_overload', 10 );

// The users list needs some extra help by overriding the user's preference.
// Otherwise, the table would list only X users, but the pagination
would be incorrect.
function my_override_user_dashboard_settings() {
	if ( is_admin() && ( $uid = get_current_user_id() ) ) {
		$user_settings = array( 'users_per_page', 'site_users_network_per_page' );
		update_user_option( $uid, 'users_per_page', *20* );
	}
}
add_action( 'init', 'my_override_user_dashboard_settings', 10 );


I have only tried this in a normal install, but I see no reason for it not
to work in a multisite install as well.

You can adjust all "20" numbers to whatever you want your users to be
limited to. Note that for the post/pages/custom post types they will still
see their preferred number in the "Screen Options" panel, but for the Users
lists, they will actually see the number that you enter(and it will be
over-ridden on every request).

That is because, when hooking to the "request" filter, the posts table
pagination adjusts automatically just fine, but the users pagination does
not.

If that doesn't suite you well enough, it will be at least a pretty good
point to start at :)

Hope that helps,
Nikola Nikolov


On Mon, Sep 30, 2013 at 11:01 PM, Jesse Friedman <me at jes.se.com> wrote:

> We are experiencing a rather large load on our servers because we have 500
> sites on a multisite network with a 100 plus users that are working all day
> long.
>
> One thing I would like to do to limit the load on the server is to globally
> control the number of posts, users, cpt's, comments, etc... that someone
> can display in their admin screen.
>
> Right now I'm sure many of my users have changed that number to show 50,
> 100 or more posts on any given screen.
>
> I would like to reset everything to display 20 items per page. Anyone have
> an easy way to do this?
>
> --
> thanks
>
> *jesse friedman*
> jes.se.com *
> *
> Book: Web Designers Guide to WordPress -
> http://wdgwp.com/onamazon<http://wdgwp.com/onamazon>
> Twitter: @professor <http://twitter.com/professor>
> Facebook: Like<
> https://www.facebook.com/pages/Jesse-Friedman/204793299545174>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list