如何过滤wordpress中管理员创建的用户

时间:2014-06-20 17:45:53

标签: wordpress

有没有办法将管理员在wordpress中创建的用户分开,例如admin1创建了3个编辑器,admin2创建了5个编辑器,admin1不应该看到admin2创建的用户,反之亦然。

2 个答案:

答案 0 :(得分:2)

请在您的functions.php中添加以下代码

        add_action( 'user_register', 'ad_registration_save', 10, 1 );
        function ad_registration_save( $user_id ) { 
                $current_admin_user = wp_get_current_user();
            if ( isset( $_POST['first_name'] ) ){
                $user = new WP_User( $user_id );
                $user_roles = $user->roles;
                $user_role = array_shift($user_roles);
                if($user_role!="administrator"){
                    update_user_meta($user_id, 'asso_admin', $current_admin_user->ID);  
                }else{
                    update_user_meta($user_id, 'asso_admin', $user_id); 
                }        
            }
        }


        add_action('pre_user_query','adminuser_pre_user_query');
        function adminuser_pre_user_query($user_search) {
            if(is_admin()){
              $user = wp_get_current_user();
              if($user->ID!=1){
                global $wpdb;
                $user_search->query_from .= "JOIN {$wpdb->usermeta} um ON um.user_id = {$wpdb->users}.ID AND um.meta_key = 'asso_admin'";
                $user_search->query_where = 'WHERE um.meta_value=' . $user->ID;         
              }
            }
        }

答案 1 :(得分:0)