Always send user to primary (WPMU) site

Users of a WordPress Multisite can configure their Primary Site, when they click the My Sites link in the WP Admin Bar as shown below.

By default, WPO365 will not take a user's primary site in consideration, because of the complexity to decide whether or not users should be redirected to their primary sites or not. However, you can override this behavior by adding the following piece of PHP code to your theme's functions.php file.

add_filter('login_redirect', function ($redirect_to, $requested_redirect_to, $wp_usr) {

    if (is_multisite() && is_user_logged_in() && $wp_usr->has_cap('read')) {
        $primary_blog = get_user_meta($wp_usr->ID, 'primary_blog', true);
        switch_to_blog($primary_blog);
        $site_url = get_home_url();
        restore_current_blog();
        return $site_url;
    }

    return $redirect_to;
}, PHP_INT_MAX, 3);

Please note Using this code snippet is at your own risk and it is not recommend that you add custom code to your theme's functions.php directly. Instead, you should create a child theme and add custom code there.
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us