Last active
October 11, 2022 21:42
-
-
Save wpsmith/c19b5107a4e3381b6e9e756cb5aca60f to your computer and use it in GitHub Desktop.
PHP: Hide User Mu-Plugin
This file contains 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
{ | |
"name": "wpsmith/hide-user", | |
"description": "Hides user in WordPress Admin.", | |
"type": "project", | |
"license": "GPLv2+", | |
"authors": [ | |
{ | |
"name": "Travis Smith", | |
"email": "t@wpsmith.net" | |
} | |
], | |
"minimum-stability": "dev", | |
"require": { | |
"wpsmith/user": "dev-master" | |
} | |
} |
This file contains 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: WPS User | |
* Plugin URI: https://wpsmith.net | |
* Description: User management. | |
* Author: Travis Smith <t@wpsmith.net> | |
* Author URI: https://wpsmith.net | |
* Text Domain: wps | |
* Domain Path: /languages | |
* Version: 0.1.0 | |
*/ | |
/** | |
* Plugin main file. | |
* | |
* @package WPS\Plugins\HideUser | |
* @author Travis Smith <t@wpsmith.net> | |
* @license GPL-2.0+ | |
* @link https://wpsmith.net/ | |
*/ | |
namespace WPS\Plugins\HideUser; | |
// Require the composer autoloader. | |
require 'vendor/autoload.php'; | |
// Use the User Package & hide hidden_user1 & hidden_user2. | |
\WPS\User\HideUser::get_instance( array( | |
'hidden_user1', | |
'hidden_user2', | |
) ); |
This file contains 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: WPS User | |
* Plugin URI: https://wpsmith.net | |
* Description: User management. | |
* Author: Travis Smith <t@wpsmith.net> | |
* Author URI: https://wpsmith.net | |
* Text Domain: wps | |
* Domain Path: /languages | |
* Version: 0.1.0 | |
*/ | |
/** | |
* Plugin main file. | |
* | |
* @package WPS\Plugins\HideUser | |
* @author Travis Smith <t@wpsmith.net> | |
* @license GPL-2.0+ | |
* @link https://wpsmith.net/ | |
*/ | |
namespace WPS\Plugins\HideUser; | |
add_action( 'pre_user_query', 'WPS\Plugins\HideUser\pre_user_query' ); | |
/** | |
* Remove user from all user queries. | |
* @global \wpdb $wpdb WordPress database abstraction object. | |
* | |
* @param \WP_User_Query $user_search The current WP_User_Query instance, | |
* passed by reference. | |
*/ | |
function pre_user_query( $user_search ) { | |
/** | |
* @var \WP_User $current_user \WP_User object for the current user. | |
*/ | |
$current_user = wp_get_current_user(); | |
if ( ! $current_user->exists() ) { | |
return; | |
} | |
// If the current user is not hidden_user1, let's remove hidden_user1. | |
if ( 'hidden_user1' !== $current_user->user_login ) { | |
global $wpdb; | |
// Now remove our hidden_user1 from the user query. | |
$user_search->query_where = str_replace( | |
'WHERE 1=1', | |
"WHERE 1=1 AND {$wpdb->users}.user_login != 'hidden_user1'", | |
$user_search->query_where | |
); | |
} | |
} |
This file contains 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: WPS User | |
* Plugin URI: https://wpsmith.net | |
* Description: User management. | |
* Author: Travis Smith <t@wpsmith.net> | |
* Author URI: https://wpsmith.net | |
* Text Domain: wps | |
* Domain Path: /languages | |
* Version: 0.1.0 | |
*/ | |
/** | |
* Plugin main file. | |
* | |
* @package WPS\Plugins\HideUser | |
* @author Travis Smith <t@wpsmith.net> | |
* @license GPL-2.0+ | |
* @link https://wpsmith.net/ | |
*/ | |
namespace WPS\Plugins\HideUser; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment