Restrict wp-admin access to certain users


/ Published in: PHP
Save to your folder(s)

Adding this snippet to the functions.php of your wordpress theme will restrict wp-admin access to certain users as defined at

http://codex.wordpress.org/Roles_and_Capabilities

And also still allows for user access to admin-ajax.php , async-upload.php .


Copy this code and paste it in your HTML
  1. function restrict_access_admin_panel(){
  2. global $current_user;
  3. get_currentuserinfo();
  4.  
  5. if (
  6. // Look for the presence of /wp-admin/ in the url
  7. stripos($_SERVER['REQUEST_URI'],'/wp-admin/') !== false
  8. &&
  9. // Allow calls to async-upload.php
  10. stripos($_SERVER['REQUEST_URI'],'async-upload.php') == false
  11. &&
  12. // Allow calls to admin-ajax.php
  13. stripos($_SERVER['REQUEST_URI'],'admin-ajax.php') == false
  14. ) {
  15.  
  16. // Does the current user fail the required capability level?
  17. if (!current_user_can('activate_plugins')) {
  18. wp_redirect( get_bloginfo('url') );
  19. }
  20. }
  21. }
  22. add_action('admin_init', 'restrict_access_admin_panel', 1);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.