Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Created November 1, 2017 01:15
Show Gist options
  • Save ricardobrg/9f0f1c053640c9725b39dbc1970d523d to your computer and use it in GitHub Desktop.
Save ricardobrg/9f0f1c053640c9725b39dbc1970d523d to your computer and use it in GitHub Desktop.
WP block wp-admin based in user role
<?php
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
//check if user is administrator
if (! current_user_can( 'administrator' ) ){
//check if it's wp-admin and not ajax calls
if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
//redirect to home
wp_redirect( home_url() );
//exit after redirect is required
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment