User Switching in the Admin Bar – Updated to V1.0

A while ago I released a the Admin Bar User Swtiching plugin which was a dependancy of John Blackbourn‘s excellent User Switching plugin. After some community feedback I needed to make some changes. Therefore I am pleased to release version 1.0.

A number of users left feedback and support requests on the WordPress.org support forums. The main issue, which I actually already knew about and stated in the plugin readme.txt file was that the plugin was potentially quite resource hungry.

In order to build the “Switch to” links in the admin bar the plugin was querying all the sites users on every page load which was OK for sites with a low number of users but not great for sites with hundreds or even thousands of users. Therefore some changes needed to be made.

One support forum post outlined the following:

Had to manually remove Plugin as it consumed all available memory and killed the frontend & WP Admin. Consider adding an AJAX search instead of full hover list (site has 80,000+ users) 🙂

Therefore I had to go about solving this issue and started down the path suggested by the commenter. This was interesting for me and I had not really used the WordPress AJAX method before and therefore found it very interesting.

Anyway after about 3 – 4 hours of Dev time I finally have version 1.0 of the Admin Bar User Switching plugin which, instead of listing all users in the admin bar dropdown, displays a search box so users can type a username (including the use of a wildcard). On submitting this search a list of users is presented and when clicked you are switched to that user.

The benefit here is that no queries are being run on every page load, only when a user runs a search for a specific user.

I hope this brings significant performance benefits to the plugin and allows sites with many users to take advantage of the plugin.

User Switching from The WordPress Admin Bar

Recently I have been working on a number of multisite installs of WordPress. The largest of these has been for a multisite install that is being used as an Intranet in a corporate environment. To help see the site from other users’ perspective I have been using the excellent User Switching plugin by John Blackbourn. However much of the switching I wanted was from the front end. This post looks at how I solved this problem.

Since WordPress 3.0 (ish) the WordPress admin bar has provided easy access to a lot of the dashboard functions such as writing new posts and pages and moderating comments etc. Along with this came the ability to easily customise the WordPress admin bar, adding your own content to it. With this in mind it seemed the obvious solution was to add User Switching to the admin bar, so that it would be enabled from the front end of the site.

Therefore I went about building a plugin to do this which you can see below:

[gist id=5007195 bump=1]

Initially I had a problem with the plugin in that I could not get the wpnonce working for the switch to user links. However excellent support from the plugin author helped me solve this problems in minutes. Thanks to John for a speedy and precise answer to the query.

The plugin works great, but if you do not have User Switching installed, the plugin checks for this and then does nothing. What I really need is for the plugin to not activate if the User Switching plugin is not installed. If anyone could help me out with this I would be very grateful.