• Resolved barzeen

    (@barzeen)


    I am using this code to redirect users to respective sub-site after login.

    <?php
    function ds_login_redirect( $redirect_to, $request_redirect_to, $user )
    {
        if ($user->ID != 0) {
            $user_info = get_userdata($user->ID);
            if ($user_info->primary_blog) {
                $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' ;
                if ($primary_url) {
                    wp_redirect($primary_url);
                    die();
                }
            }
        }
        return $redirect_to;
    }
    add_filter('login_redirect','ds_login_redirect', 100, 3);
    ?>

    How can I modify it to allow users that are already logged in to be redirected, if they try to access the primary site or the wrong-sub site, to their respective sub-site

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    You can’t with that code. That code hooks into login_redirect 🙂 Once the login action is done, it won’t get called again.

    What you’re doing, you would need to have an mu-plugin that checks, on every page load, if the user is a member of that site and if not, redirect them to their site.

    Thread Starter barzeen

    (@barzeen)

    So does anyone know how to implement this code that the gentleman is speaking of to check if users are a member of the site and if not, redirect them to their site?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    I’m a lady 🙂

    http://codex.wordpress.org/WPMU_Functions/is_user_member_of_blog is the function you want.

    Thread Starter barzeen

    (@barzeen)

    Thanks a lot. How do insert properly redirect into that code? Sorry I don’t know php?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Oh, you need someone to write the code for you. That’s different. I assumed that since you had the other code, you knew a little.

    Untested, but the basic idea is this:

    <?php
        global $current_user;
        get_currentuserinfo();
    
        if ( !is_user_member_of_blog( $current_user->ID ) ) {
           $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' ;
           wp_redirect($primary_url);
           die();
        }
    ?>

    This says ‘If the current user ID is not a member of this site, redirect them to their primary site.’ You may want to wrap an if (is_user_logged_in()) { } around it, to stop non-logged in users from running that check, and send the non logged in to the main URL.

    Thread Starter barzeen

    (@barzeen)

    I added the code like this, but it doesn’t seem to do anything:

    <?php
    include('/home/scholast/public_html/wp-includes/pluggable.php');
    function member_redirect()
    {
    if(is_user_logged_in()) {
        global $current_user;
        get_currentuserinfo();
    
        if ( !is_user_member_of_blog( $current_user->ID ) ) {
           $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' ;
           wp_redirect($primary_url);
           die();
        }
    }}
    ?>

    Thread Starter barzeen

    (@barzeen)

    I put it in the mu-plugins folder and called it member_redirect.php

    I don’t know what I am doing really, just guessing.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Remove this line:

    include('/home/scholast/public_html/wp-includes/pluggable.php');

    You got most of the way there. The part your missing is calling the function.

    Functions say ‘Do this.’

    Actions say ‘Do that function NOW.’

    In your case, you could get away with just this:

    <?php
    
    if(is_user_logged_in()) {
        global $current_user;
        get_currentuserinfo();
    
        if ( !is_user_member_of_blog( $current_user->ID ) ) {
           $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' ;
           wp_redirect($primary_url);
           die();
        }
    }

    (and yes, I left off a closing php call on purpose)

    Thread Starter barzeen

    (@barzeen)

    The reason I added the function, was because I keep getting this error:

    Fatal error: Call to undefined function is_user_logged_in() in /home/scholast/public_html/wp-content/mu-plugins/member_redirect.php on line 3

    Once again I just created a blank php file in my “mu-plugins” directory and pasted the code in. I don’t if I should include it on my theme’s main page template, or footer, or header or what.

    I really appreciate your help by the way Mika.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Nothing.

    The file in mu-plugins will automatically run, just like a plugin. It’s like a functions.php for your network.

    Are you still getting that error?

    Thread Starter barzeen

    (@barzeen)

    yes

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Huh, it’s running before the function is defined. That’s odd.

    Okay, put it back in the function, but then have it run in wp_head

    http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

    Thread Starter barzeen

    (@barzeen)

    well added a function so it doesn’t give me an error, but it doesn’t seem to work. I can acess any page of the other sub-sites without being redirected.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Did you also add the action?

    Thread Starter barzeen

    (@barzeen)

    Finally finally finally I got it to work. Thanks for the help Ipstenu. I am mind blow why no body has made a plugin for this. Anyways, you seem very active so maybe you know where or how to share this. I don’t know why I couldn’t get $user_info->primary_blog to output anything. So I used get_active_blog_for_user instead. Work’s great now. The wp_head action is the key.

    <?php
    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');
    ?>

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘force redirect users to respective sub-site’ is closed to new replies.