Skip to content

Instantly share code, notes, and snippets.

@erikjoling
Created July 31, 2018 11:37
Show Gist options
  • Save erikjoling/2e1ebb0235b9c309b227eed05b970134 to your computer and use it in GitHub Desktop.
Save erikjoling/2e1ebb0235b9c309b227eed05b970134 to your computer and use it in GitHub Desktop.
Restricting the WordPress admin-area to a specified user
<?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