Limit Users Access To Their Respective Website In WordPress

A few hours back, we have discussed about automatically redirecting users to their specific website, as soon they login to your WordPress multisite network.

But once a successful login is made, it won’t get called again with that code and thereafter if any user attempts to get access to other’s website, he will easily get it.

So how to enforce redirection to logged-in user’s respective website? In this lesson you will learn about getting this job done with an easy PHP function.

This can added either in your theme’s function.php file or via Code Snippets plugin.

function redirect() {
 if(is_user_logged_in()&& ($user->ID = 1)) {
	global $current_user;
 	get_currentuserinfo();
 if ( !is_user_member_of_blog( $current_user->ID ) ) {
                $blog = get_active_blog_for_user($current_user->ID);
		$blog_url = $blog->siteurl;
                wp_redirect ($blog_url);
    }}}
add_action('wp_head', 'redirect');

Given code forcefully directs users to their respective website. It makes users that are already logged-in  on your WordPress multisite network to be redirected to their website.

Even if they try to access the primary website or any other subsite, they will get automatically redirected to the homepage of their respective website.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.