Last active
June 20, 2017 22:35
-
-
Save luisabarca/39dffb5bc1ebc587b0bb79b0c738409d to your computer and use it in GitHub Desktop.
Users registered on the main site can't login on subsites on a multisite installation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: bPlus User per site | |
*/ | |
// Url to redirect | |
define( 'BP_DEFAULT_URL', 'http://superpositif.com/interne/'); | |
function is_valid_user_login( $user = null ) { | |
// Current site | |
$current_site_id = get_current_blog_id(); | |
if ( is_null( $user ) ) { | |
$user = wp_get_current_user(); | |
} | |
if ( is_user_member_of_blog( $user->id, $current_site_id ) ) { | |
return true; | |
} | |
return false; | |
} | |
function my_check_user_blogs( $user_login, $user ) { | |
if ( current_user_can( 'edit_posts' ) ) { | |
return false; | |
} | |
// check if user is in the current site | |
if ( ! is_valid_user_login( $user ) ) { | |
wp_redirect( BP_DEFAULT_URL ); | |
exit(); | |
} | |
} | |
add_action('wp_login', 'my_check_user_blogs', 10, 2); | |
function my_set_default_blog_to_user( $user_id ) { | |
// Subscribers are added to default blog | |
if ( ! user_can( $user_id, 'edit_posts' ) ) { | |
add_user_to_blog(1, $user_id, 'subscriber' ); | |
} | |
} | |
add_action( 'wpmu_new_user', 'my_set_default_blog_to_user' ); | |
function my_check_user_access_for_site() { | |
// check logged in users only | |
if ( ! is_user_logged_in() ) { | |
return false; | |
} | |
// dont perform check on normal users | |
if ( current_user_can( 'edit_posts' ) ) { | |
return false; | |
} | |
if ( ! is_valid_user_login() ) { | |
wp_redirect( BP_DEFAULT_URL ); | |
exit(); | |
} | |
} | |
add_action( 'init', 'my_check_user_access_for_site' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment