Redirect users from WordPress admin pages

Posted on Apr 20, 2011 in Wordpress |


After some time looking at the various user-restriction/membership style plugins, I realized that there wasn’t a quick and easy way to ban normal users from all of the wp-admin pages.

Fortunately, wordpress makes this sort of customization easy with its amazing API. These four lines of code are all that’s needed:

function my_wp_admin_ban(){
	if( !current_user_can('activate_plugins') ){
		wp_redirect( get_bloginfo('wpurl') );
		exit;
	}
}
add_action('admin_init','my_wp_admin_ban');

You can get this working in two ways:

  1. Copy the contents above into a file named wp-admin-ban.php (put them between ) and move it to wp-contents/mu-plugins folder. If the mu-plugins folder doesn’t exist, create it. This will be available across any theme on your site.
  2. Copy the code to your theme’s functions.php file, which restricts functionality to that theme.

If you can think of a good membership plugin on the wordpress repo that does the trick, feel free to drop me a line!

On that note, here’s two plugins you might find useful:

  • My own ajax login plugin so users can log in and register with smooth ajax effects from the front of your site!
  • Membership Plugin If you’re looking for a good premium membership plugin with great support (personally recommended, tried a few others before it). They also have a lite free version for you to try out.