• I am trying to redirect users to their personal blog in Multisite.

    I saw the variables:
    [variable]username[/variable]
    [variable]homeurl[/variable]
    [variable]siteurl[/variable]
    (and more …)

    The only one that works for me is “username”. Unfortunately some people might not have the same username as their blog so that won’t work.

    I tried making my own variable but I kept getting an error.

    Do you have any suggestions?

    My Code:
    $theUserName = rawurlencode( $user->user_login );
    $theUser = get_user_id_from_string( $theUserName );
    return get_blogaddress_by_id( $theUser );

    The Error message:
    Cannot modify header information – headers already sent by (output started at … wp-includes/pluggable.php on line 876″

    Thanks!

    https://www.ads-software.com/plugins/peters-login-redirect/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Is it possible to make login into 2 or more category?

    Thread Starter kater89

    (@kater89)

    Not really … Each user will have their own multisite profile that they should be directed to. Really there are two options that I see …

    1) I could use their website that I have imputed in their account (On their user page it’s one of the boxes like the Username / Nickname etc…)

    2) Have users get directed to the first blog that they are registered to. (Preferred)

    Did you found a solution to this?

    Thread Starter kater89

    (@kater89)

    Not really – I ended up using a work around for the time being but it takes more work every time I add a user.

    For the “All other Users” section, I added this for the URL:
    [variable]username[/variable]

    When I add a user to the blog, I try and make the username the same as the blog name and then it redirects perfectly.

    If the user doesn’t have the same username as the blog, I manually put them in using the “specific users” section of the plugin.

    I don’t particularly like this method because I have a lot of users that don’t have the same username as their blog so the list is quite long.

    Hope that helps for now and maybe there will be an update later that will fix this.

    2) Have users get directed to the first blog that they are registered to. (Preferred)

    I found a way to do this. Peter wisely put a filter on the variable name so we can create our own variables.

    Add this to your theme’s functions.php:

    function users_first_blog( $bool, $variable, $user )
    {
    	if( 'first_blog' == $variable )
    	{
    		$blogs = get_blogs_of_user( $user->ID );
    		$first_blog = array_shift( $blogs );
    		return $first_blog->path;
    	}
    }
    
    add_filter( 'rul_replace_variable', 'users_first_blog', 10, 3 );

    (Alternatively, we can make it into a mini-plugin. Let me know if you want to do that instead.)

    This creates a variable called first_blog which contains the relative path of the first blog the user was added to.

    One hitch: For this to work, we have to hack the plugin to work with multi-site by changing this line

    $rul_db_addresses = $wpdb->prefix . 'login_redirects';

    to this

    $rul_db_addresses = $wpdb->base_prefix . 'login_redirects';

    On the settings page for the main site, use that new variable name in the All other users fields like so:
    URL: https://mysite.com[variable]first_blog[/variable]

    I can put a demo online if you want to see it in action first.

    Thread Starter kater89

    (@kater89)

    Awesome thanks! That works great and is exactly what I needed!

    One thing i noticed though is I just needed to put

    [variable]first_blog[/variable]

    and not

    https://mysite.com[variable]first_blog[/variable]

    Cool. Thanks for the update. I’m working on an add-on plugin to Peter’s plugin and I will include this function.

    It’s nice when a topic ends up with an useful plugin.

    But I have a problem with Chris Dillon solution on a subdomain install. It redirects to the homepage of the main website (not users site).

    On a localhost install where I have subfolders, it’s working.

    @mkdev Thanks for pointing that out. Here’s a function that works with subdomain or subdirectory multi-site. This supercedes the version above.

    function plr_first_blog( $bool, $variable, $user )
    {
      if( 'first_blog' == $variable )
      {
        $blogs = get_blogs_of_user( $user->ID );
        $first_blog = array_shift( $blogs );
        if( MULTISITE )
        {
          if( SUBDOMAIN_INSTALL )
            return $first_blog->siteurl;
          else
            return $first_blog->path;
        }
      }
    }
    add_filter( 'rul_replace_variable', 'plr_first_blog', 10, 3 );

    And thanks @kater89,

    [variable]first_blog[/variable]

    should be used. In a subdomain install, an absolute path is returned; in a subdirectory install, a relative path is returned.

    I have tested this with a blank redirect field and alongside the other built-in variables http_referer and siteurl and it all works on my machine ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘redirect to user's main blog – MultiSite’ is closed to new replies.