Skip to content

Instantly share code, notes, and snippets.

@cgrymala
Created February 12, 2015 20:28
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 cgrymala/a203590f8cbf9b16c4e5 to your computer and use it in GitHub Desktop.
Save cgrymala/a203590f8cbf9b16c4e5 to your computer and use it in GitHub Desktop.
Keep Subscribers Out of the WP Admin Area
<?php
add_action( 'admin_init', 'hide_backend_from_user' );
/**
* Hide the backend from specific user levels
* @uses VFH_User_Controls::$options['login_redirect_to'] to determine where the user should be redirected
* @uses VFH_User_Controls::$user_levels to see if the user needs to be redirected
*/
function hide_backend_from_user( $level='edit_posts' ) {
/**
* Attempt not to short-circuit AJAX requests
*/
if ( function_exists( 'doing_ajax' ) && doing_ajax() ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( ! is_user_logged_in() )
return;
if ( ! current_user_can( $level ) ) {
wp_safe_redirect( home_url() );
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment