Skip to content

Instantly share code, notes, and snippets.

@JRMorris77
Last active January 10, 2017 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRMorris77/2bed73b3324e703d5a09c2351f6936c9 to your computer and use it in GitHub Desktop.
Save JRMorris77/2bed73b3324e703d5a09c2351f6936c9 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Simple Login Redirect
Plugin URI: https://premium.wpmudev.org/support/
Description: A simple mu-plugin that redirects users to their subsite on login.
Version: 0.0.1
Author: James Morris - WPMU DEV
Author URI: https://premium.wpmudev.org/profile/jrmorris77
Network: true
*/
function my_login_redirect( $redirect_to, $request, $current_user ) {
$current_user = wp_get_current_user();
if ( $current_user ) {
$user_id = $current_user->ID;
}
if ( isset( $user_id ) ) {
// Get the blogs of this user
$user_blogs = get_blogs_of_user( $user_id );
// Get the URL of the blog
foreach ( $user_blogs AS $user_blog ) {
if ( !isset( $userblogid ) ) {
$userblogid = $user_blog->userblog_id;
return $user_blog->domain;
}
}
// If user has no blog, return to the homepage.
} else {
return home_url();
}
}
add_filter( "login_redirect", "my_login_redirect", 10, 3 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment