Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * The redirect mu-plugin
- * Redirects user to their primary blog admin once they log in (instead of main site dashboard)
- * Redirects user to their primary blog admin when they try to access wp-admin of some other blog
- */
- class itg_wpms_login_redirect {
- /**
- * Constructor
- * Hooks the actions
- */
- public function __construct() {
- add_action('login_redirect', array(&$this, 'login_redirect'), 100, 3);
- add_action('admin_menu', array(&$this, 'admin_redirect'));
- }
- /**
- * Redirect the users on login
- * Users should be redirected to their primary site whenever they try to login from anywhere
- *
- * Should be hooked
- *
- * @param string $redirect_to
- * @param string $requested_redirect_to
- * @param string $user
- * @return string
- */
- public function login_redirect($redirect_to, $requested_redirect_to, $user) {
- if ($user->ID != 0 && $user->ID != 1) {
- $user_info = get_userdata($user->ID);
- if ($user_info->primary_blog) {
- $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
- if ($primary_url) {
- wp_redirect($primary_url);
- die();
- }
- }
- }
- return $redirect_to;
- }
- /**
- * Redirect the logged in users to their primary site's admin
- * when they try to access the admin of the main site or other site
- *
- * Comment out line 61 to block only the main site admin
- * @global type $current_user
- * @global type $blog_id
- * @return type
- */
- public function admin_redirect() {
- global $current_user, $blog_id;
- if ($current_user->ID == 0 || $current_user->ID == 1)
- return;
- //the below line blocks access ONLY to the main site wp-admin
- //remove the line to block access to all sites except primary
- if ($blog_id !='1') return;
- $primary_url = get_blogaddress_by_id($current_user->primary_blog) . 'wp-admin/';
- if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') && ( $blog_id != $current_user->primary_blog)) {
- wp_redirect($primary_url);
- }
- }
- }
- $itg_wpms_login_redirect = new itg_wpms_login_redirect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement