SHARE
TWEET

Untitled

a guest Jun 19th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: bPlus User per site
  4. */
  5.  
  6. function is_valid_user_login( $user = null ) {
  7.     // Current site
  8.     $current_site_id = get_current_blog_id();
  9.  
  10.     if ( is_null( $user ) ) {
  11.         $user = wp_get_current_user();
  12.     }    
  13.  
  14.     $sites = get_blogs_of_user( $user->id );
  15.  
  16.     //var_dump($sites);
  17.     //die;
  18.  
  19.     if ( is_user_member_of_blog( $user->id, $current_site_id ) ) {
  20.         return true;        
  21.     }
  22.  
  23.     return false;
  24. }
  25.  
  26. function my_check_user_blogs( $user_login, $user ) {
  27.     if ( current_user_can( 'manage_options' ) ) {
  28.         return false;
  29.     }
  30.  
  31.     // check if user is in the current site
  32.     if ( ! is_valid_user_login( $user ) ) {
  33.         wp_logout();
  34.         wp_redirect( get_bloginfo('url') );
  35.         exit();
  36.     }
  37.  
  38. }
  39.  
  40. add_action('wp_login', 'my_check_user_blogs', 10, 2);
  41.  
  42.  
  43. function my_set_default_blog_to_user( $user_id ) {
  44.     add_user_to_blog(1, $user_id, 'subscriber' );
  45. }
  46.  
  47. add_action( 'wpmu_new_user', 'my_set_default_blog_to_user' );
  48.  
  49. function my_check_user_access_for_site() {
  50.     // check logged in users only
  51.     if ( ! is_user_logged_in() ) {
  52.         return false;
  53.     }
  54.  
  55.     // dont perform check on admins
  56.     if ( current_user_can( 'manage_options' ) ) {
  57.         return false;
  58.     }
  59.  
  60.     if ( ! is_valid_user_login() ) {
  61.         // log out
  62.         wp_logout();
  63.         wp_redirect( get_bloginfo('url') );
  64.         exit();
  65.     }
  66. }
  67.  
  68. add_action( 'init', 'my_check_user_access_for_site' );
RAW Paste Data
Top