Forums

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

  1. Driftless1
    Member
    Posted 4 months 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 4 months 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 4 months ago #

    Brilliant!

    Where were you about 2 days ago ;)

    Thanks for this. Anyone else have a similar solution?

  4. Driftless1
    Member
    Posted 4 months 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 4 months ago #

    Johnnytee:

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

  6. jimgroom
    Member
    Posted 4 months 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
    Member
    Posted 4 months ago #

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

  8. DeannaS
    Member
    Posted 4 months 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 4 months ago #

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

  10. Driftless1
    Member
    Posted 4 months 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 3 months ago #

    any news?

  12. dpd1998
    Member
    Posted 3 months ago #

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

  13. DeannaS
    Member
    Posted 3 months 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 2 months 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
    Member
    Posted 2 months ago #

    Did you put php tags around it?

  16. JsonB123
    Member
    Posted 2 months 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 :-/.

Reply

You must log in to post.

About this Topic