WordPress.org

Support

Support » Multisite » [Resolved] force redirect users to respective sub-site

[Resolved] force redirect users to respective sub-site

Viewing 15 replies - 1 through 15 (of 15 total)
  • 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.

    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?

    I’m a lady 🙂

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

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

    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.

    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();
        }
    }}
    ?>

    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.

    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)

    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.

    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?

    yes

    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

    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.

    Did you also add the action?

    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 ‘[Resolved] force redirect users to respective sub-site’ is closed to new replies.
Skip to toolbar