Member Redirect Multisite Header Problems

I am trying to redirect users that are logged in to my multisite. I want a user to only access the site they are a member of. The code checks if a logged in user is an actual member of the one of the sites in the network. If they are not, they are redirected to their main site. I created code that used to work, but now I get that header error that I can’t run the function because something else controls the header. The header error is: Warning: Cannot modify header information etc etc

The code is as follows:



function member_redirect() {

if (is_user_logged_in()&& ($user->ID = 1)) {

global $current_user;


if ( !is_user_member_of_blog( $current_user->ID ) ) {

$primary_url = get_blogaddress_by_id($current_user->ID) . ” ;

$blog = get_active_blog_for_user($current_user->ID);

$blog_url = $blog->siteurl;

wp_redirect ($blog_url);


add_action (‘wp_head’ , ‘member_redirect’:wink:;


Maybe there is a way to run it outside the header. It doesn’t run at all if I dont throw in wp_head.