Skip to content

Instantly share code, notes, and snippets.

@petersplugins
Created December 20, 2016 12:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petersplugins/e427d4b6978a50b75954607de707d701 to your computer and use it in GitHub Desktop.
Save petersplugins/e427d4b6978a50b75954607de707d701 to your computer and use it in GitHub Desktop.
<?php
// This code snippet blocks WordPress dashboard access for non admin users
// Add a custom function to check if the user is allowed to access the admin area
add_action( 'init', 'block_admin_access' );
function block_admin_access() {
if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
// Redirect to home page if a non admin user tries to access admin area
wp_redirect( home_url() );
exit;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment