The MU forums have moved to WordPress.org

FINALLY! Redirect user to Primary blog on login (22 posts)

  1. Driftless1
    Member
    Posted 14 years ago #

    I need(ed) a way to redirect my users to their primary blog when they try to log into the main blog (where they are subscribers).

    Search as I might, no-one seemed to have an answer beyond telling the people who asked to "search for the answer." (This has been going on for several years apparently.)

    So, I present to you a simple core hack (that IMHO should be the default behavior) to redirect users to their Primary blog instead of the profile.php on the main blog (as is the default.)

    Modify wp-login.php at line 485:

    if ( !is_wp_error($user) ) {
    		// If the user can't edit posts, send them to their profile.
    		if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
    				$primary = get_usermeta($user->ID , 'primary_blog'); 	//get primary blog ID
    				if ($primary && $primary != "1"){  						//if the users have a primary blog, and its not the main blog... (may not be needed on all installs)
    					$redirect_one = get_blogaddress_by_id( $primary );  //get their primary blog
    					$redirect_to = $redirect_one ."wp-admin";  			//tack on the wp-admin page
    					} else
    					$redirect_to = admin_url('profile.php'); 			// otherwise, do the "send to profile" thing that is default
    
    			}
    		wp_safe_redirect($redirect_to);
    		exit();
    	}

    1) Anyone see a fatal problem with this? (so far it seems to be working quite well)

    2) If anyone has a more elegant way to do this, please let me know.

    3) For those searching desperately for a way to do this, happy hacking.

  2. johnnytee
    Member
    Posted 14 years ago #

    I only want my users to see there Primary Blog, I use this method, no core hacking

    // redirect to primary blog
    function redirect_to_primary_blog()  {
    	if(!is_site_admin()){
       	global $current_user;
    	get_currentuserinfo();
    	$blog_details = get_blog_details(get_usermeta($current_user->ID,'primary_blog'));
    	if($_SERVER['HTTP_HOST'] != $blog_details->domain){
    		header( 'Location: http://'.$blog_details->domain.'/wp-admin/' ) ;
    	}
    	}
    }
    
    add_action('admin_init', 'redirect_to_primary_blog');
  3. Driftless1
    Member
    Posted 14 years ago #

    Brilliant!

    Where were you about 2 days ago ;)

    Thanks for this. Anyone else have a similar solution?

  4. Driftless1
    Member
    Posted 14 years ago #

    Actually -- after trying it out, that only works if your blogs are in subdomains, mine are in sub-directories.

    I tried a quick change, but couldn't make it work.

    Thanks though.

  5. anointed
    Member
    Posted 14 years ago #

    Johnnytee:

    What file and location did you apply your code edit to in order to get this working?

  6. jimgroom
    Member
    Posted 14 years ago #

    I bump anointed's question, where did you include this? Is it a mu-plugin? That would make sense to avoid core hacks I imagine.

  7. andrea_r
    Moderator
    Posted 14 years ago #

    Yeah, I;d put it in mu-plugins.

  8. DeannaS
    Member
    Posted 14 years ago #

    Why aren't you just using the login_redirect filter? Here's a good blog post by someone else on how to do it:

    http://nathany.com/developer/redirecting-wordpress-subscribers/

    Drop that code in an mu-plugin and away you go.

  9. Ovidiu
    Member
    Posted 14 years ago #

    if someone puts it all togetehr in a plugin, please psot a link ehre, I am interested too.

  10. Driftless1
    Member
    Posted 14 years ago #

    Thanks for the lead DeannaS - I'll see if I can bend it to my will and make a plugin.

  11. Ovidiu
    Member
    Posted 14 years ago #

    any news?

  12. dpd1998
    Member
    Posted 14 years ago #

    Where does one place the code in Johnnytee's post to make the redirect work?

  13. DeannaS
    Member
    Posted 14 years ago #

    In one of the iterations of the infinite loop bug that was fixed in 2.8.3, we had this behavior going. But, instead of fulling testing and holding off on the 2.8.3 release, we decided to pull it and just go with fixing the infinite loop. If people want it, someone should put an enhancement request into trac. My guess is that it could get worked into the next release if it's desired by enough folks.

  14. JsonB123
    Member
    Posted 14 years ago #

    Hi, just stumbled upon this one. I've used Driftless1's code in the wp-login.php file with no problems...but johnnytee's isn't working for me. I tried to put it in the mu-plugins folder and also tried in the regular plugins folder activated site-wide.

    I'd like to avoid core-hacking...so why isn't that code chunk working that johnnytee provided? I'm using WordPress MU 2.8.4a, btw.

  15. andrea_r
    Moderator
    Posted 14 years ago #

    Did you put php tags around it?

  16. JsonB123
    Member
    Posted 14 years ago #

    @ andrea_r, yeah I did :D. I think it was because it wasn't catching for me on the if($_SERVER['HTTP_HOST'] != $blog_details->domain){ bit...still not sure what that does actually?

    I just removed that part :-/.

  17. badkarma9000
    Member
    Posted 14 years ago #

    I used Driftless's code with no problems. Any idea how I can modify it to re-direct users to the front-end of the users primary site as opposed to the admin page or profile page?

  18. andrea_r
    Moderator
    Posted 14 years ago #

    Take out the part that says /wp-admin/ ? :D

  19. badkarma9000
    Member
    Posted 14 years ago #

    Just that line as a whole? So it'd be:

    if ( !is_wp_error($user) ) {
    		// If the user can't edit posts, send them to their profile.
    		if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
    				$primary = get_usermeta($user->ID , 'primary_blog'); 	//get primary blog ID
    				if ($primary && $primary != "1"){  						//if the users have a primary blog, and its not the main blog... (may not be needed on all installs)
    					$redirect_one = get_blogaddress_by_id( $primary );  //get their primary blog
    
    					} else
    					$redirect_to = admin_url('profile.php'); 			// otherwise, do the "send to profile" thing that is default
    
    			}
    		wp_safe_redirect($redirect_to);
    		exit();
    	}

    Did I do that right?

  20. Driftless1
    Member
    Posted 14 years ago #

    No... I don't think so...

    Simplest to just get rid of the ."wp-admin" tag on - so the URL it issues is is the primary blog address (called by the line above)

    Should look like this:

    $redirect_one = get_blogaddress_by_id( $primary );  //get their primary blog
    					$redirect_to = $redirect_one ;  			//send them to their primary (you can tack on anything here you want)
    					} else
    					$redirect_to = admin_url('profile.php'); 			// otherwise, do the "send to profile" thing that is default
  21. callumalden
    Member
    Posted 14 years ago #

    fantastic- i also think this should be the default for users logging in. this should be included as at least an option in the admin screen, no?

  22. bjorsq
    Member
    Posted 13 years ago #

    Nice pointers here but this is the only way I could get it to work (using 2.9.2)

    <?php
    // redirect users to their primary blog
    function change_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) {
                    //redirect to primary blog
                    wp_redirect($primary_url);
                    die();
                }
            }
        }
        return $redirect_to;
    }
    // add filter with low priority (100), filter takes (3) parameters
    add_filter('login_redirect','change_login_redirect', 100, 3);
    ?>

    Drop this file in the mu-plugins folder.

About this Topic

  • Started 14 years ago by Driftless1
  • Latest reply from bjorsq