WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] force redirect users to respective sub-site (16 posts)

  1. barzeen
    Member
    Posted 1 year ago #

    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

  2. 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.

  3. barzeen
    Member
    Posted 1 year ago #

    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?

  4. barzeen
    Member
    Posted 1 year ago #

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

  5. 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.

  6. barzeen
    Member
    Posted 1 year ago #

    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();
        }
    }}
    ?>
  7. barzeen
    Member
    Posted 1 year ago #

    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.

  8. 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)

  9. barzeen
    Member
    Posted 1 year ago #

    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.

  10. 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?

  11. barzeen
    Member
    Posted 1 year ago #

    yes

  12. 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

  13. barzeen
    Member
    Posted 1 year ago #

    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.

  14. Did you also add the action?

  15. barzeen
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic