Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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