Advertisement
Guest User

Untitled

a guest
Apr 13th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * The redirect mu-plugin
  5. * Redirects user to their primary blog admin once they log in (instead of main site dashboard)
  6. * Redirects user to their primary blog admin when they try to access wp-admin of some other blog
  7. */
  8. class itg_wpms_login_redirect {
  9.  
  10. /**
  11. * Constructor
  12. * Hooks the actions
  13. */
  14. public function __construct() {
  15. add_action('login_redirect', array(&$this, 'login_redirect'), 100, 3);
  16. add_action('admin_menu', array(&$this, 'admin_redirect'));
  17. }
  18.  
  19. /**
  20. * Redirect the users on login
  21. * Users should be redirected to their primary site whenever they try to login from anywhere
  22. *
  23. * Should be hooked
  24. *
  25. * @param string $redirect_to
  26. * @param string $requested_redirect_to
  27. * @param string $user
  28. * @return string
  29. */
  30. public function login_redirect($redirect_to, $requested_redirect_to, $user) {
  31. if ($user->ID != 0 && $user->ID != 1) {
  32. $user_info = get_userdata($user->ID);
  33. if ($user_info->primary_blog) {
  34. $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
  35. if ($primary_url) {
  36. wp_redirect($primary_url);
  37. die();
  38. }
  39. }
  40. }
  41. return $redirect_to;
  42. }
  43.  
  44. /**
  45. * Redirect the logged in users to their primary site's admin
  46. * when they try to access the admin of the main site or other site
  47. *
  48. * Comment out line 61 to block only the main site admin
  49. * @global type $current_user
  50. * @global type $blog_id
  51. * @return type
  52. */
  53. public function admin_redirect() {
  54. global $current_user, $blog_id;
  55.  
  56. if ($current_user->ID == 0 || $current_user->ID == 1)
  57. return;
  58.  
  59. //the below line blocks access ONLY to the main site wp-admin
  60. //remove the line to block access to all sites except primary
  61. if ($blog_id !='1') return;
  62.  
  63. $primary_url = get_blogaddress_by_id($current_user->primary_blog) . 'wp-admin/';
  64.  
  65. if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') && ( $blog_id != $current_user->primary_blog)) {
  66. wp_redirect($primary_url);
  67. }
  68. }
  69. }
  70.  
  71. $itg_wpms_login_redirect = new itg_wpms_login_redirect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement