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.

WordPress Front End Profile Plugin

Have you ever worked on a site where you have lots of users on the site but they should never need to go into the WordPress admin area? Maybe you have different roles setup to show them different types of content. However you would like them to be able to edit the information on their profile page? Well now you can allow them to do this without going near the WordPress admin buy using my new WP Front End Profile plugin. Lets take a look at it.

Put simply this plugin provides you with a function for which you can add to your themes template files. When you do this the function outputs a profile editor on the front end. The function in question is:

wpfep_show_profile();

Once you add this you will be presented on the front end, in whichever template you added it to with the following profile editing screen.

Screen Shot 2014-12-06 at 11.15.28

As you can see, by default the profile editor has two tabs named Profile and Password. Each tab contains a set of fields which the user can edit. The profile tab has the majority of things that can be edited on the profile admin screen, such as first and last name, URL, email address and the bio or description. The password tab allows the user to change their password, entering it twice to confirm.

All that sounds great, but that it not really the best part of the plugin, this comes in the form of its extensibility. The plugin is built in a way that easily allows developers to add their own tabs and their own fields, either to tabs they have added or to existing tabs. What is also good, is that all of the saving is taken care of for you. Lets took at look how.

Adding Your Own Tabs

To do this you will need to use the wpfep_tabs filter. An example of how to add a new tab is given below:

https://gist.github.com/wpmark/7e38e5114ecc153f400b

As you can see you declare a number of tab args when adding the tab. Perhaps the most important of these in the Tab ID. This is used for adding fields to your tab. So lets take a look at how to add fields to your tabs.

Adding Fields to a Tab

All fields that you add are added to the user as user meta (you can use the wpfep_reserved_ids filter to save to the user table rather than user meta but that is for another day!). Fields are added to a tab using a dynamic filter. The filter is dynamic as it uses the tab ID from when the tab was added. The filter takes the following format:

wpfep_fields_$tab_id

Here the tab id is the ID of any added tab. The ID of the default tabs are profile and password. The ID of the tab we added above is wpmark_tab. Therefore to add fields to this tab we would use a filter named:

wpfep_fields_wpmark_tab

The code below is an example of how we do this:

https://gist.github.com/wpmark/1e57902ba32a0f115db7

Here we have added a select input setting the type to select and adding and options arg to give the different select options. Other types you could add include WYSIWYG, text, textarea, checkbox, password and email.

Actions and Filters Available

There are also a number of actions and filters that developers can use to modify things such as password length requirements and whether or not to use the plugins built in styles and javascript. Take a look at the Wiki on Github in order to find out more.

You can download the WP Front End Profile Plugin in the WordPress.org plugin repository or view the code and submit issues on Github.

WordPress Admin Bar User Switching

For a while now I have been using the excellent user switching plugin from John Blackbourn to easily switch between user accounts in WordPress. I have extended the plugin to produce Admin Bar User Switching.

User Switching provides an easy Switch to link in the users profile page when logged in as an administrator. This easily allows you to login as a user and see what they would be seeing. I found this particularly useful when building an Intranet for a school. As there was over 200 users of the system, all with different roles and capabilities it meant that I could easily test the different user levels without having to login and out all the time.

However it was when developing a Support Hub for Pixel Junction that I thought that there could be an easier way if switching between users than having to go to their profile screen each time. I then thought of using the admin bar as this is on the screen all the time, front and back end.

Admin Bar User Switching was born, which is an extension to the User Switching plugin which must be installed and activated before using Admin Bar User Switching. The plugin adds a “Switch To User” link in the admin bar which, when hovered displays a dropdown list of the sites user. Clicking a user switches to that user. The link then changes to “Switch Back” to allow you to go back to your own user. This is probably only useful for sites where there are less than 20 users or the list would just be too long. Maybe in the future I can categorise the users into their roles in the menu – or maybe A – Z on surname or something.

Screen Shot 2014-11-26 at 11.46.58

Feel free to download Admin Bar User Switching from the WordPress.org repository or you can follow the plugin on Github.

Remove Dashboard Menus for Specific Users

For a while now I have been using some simple code to remove the dashboard menus in WordPress, primarily so that clients can’t change certain settings and ‘break’ their sites. However the code I was using also removed those items for me too. This meant I had to memorise the admin URLs and visit them manually which is not ideal. Today I have managed to solve this problem by hiding WordPress dashboard menus for all except specified users.

The trick is to wrap your code around an if statement that first checks who the user is. The code I am using is below:

https://gist.github.com/wpmark/8160972

Basically the code grabs the current users user ID and then compares it to a list of allowed users. If is matches the function returns nothing, if it doesn’t match then it removes the specified menus in the WordPress admin areas.

In this example users with the ID 1 and 2 can see all menus, however the WordPress links menu will be removed for all other users. It is important to note that this code only removes the menus from view and doesn’t stop users from actually viewing these pages. If they know the URLs they will still load.