Created
July 31, 2018 11:37
-
-
Save erikjoling/2e1ebb0235b9c309b227eed05b970134 to your computer and use it in GitHub Desktop.
Restricting the WordPress admin-area to a specified user
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 | |
add_action( 'init', 'ejo_allow_user_only' ); | |
/** | |
* Restrict Admin section to specified username | |
* | |
* Prevent people from using the admin-section. | |
* Only allow user with username `REPLACE_THIS` | |
* | |
* WordPress 4.7.0+ | |
*/ | |
function ejo_allow_user_only() { | |
// Skip if no admin section or if doing an ajax request | |
if ( ! is_admin() ) || wp_doing_ajax() ) { | |
return; | |
} | |
// Shut the door for users who don't match username | |
$current_user = wp_get_current_user(); | |
if ( $current_user->user_login != 'REPLACE_THIS' ) { | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment