Don't allow subscribers access to the WP Dashboard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: No Dashboard | |
Description: Don't allow subscribers to access to the wp-dashboard | |
Author: Christopher Davis | |
Author URI: http://christopherdavis.me | |
*/ | |
register_activation_hook( __FILE__, 'wpse43054_activation' ); | |
function wpse43054_activation() | |
{ | |
$role = get_role( 'subscriber' ); | |
if( $role ) $role->remove_cap( 'read' ); | |
} | |
register_deactivation_hook( __FILE__, 'wpse43054_deactivation' ); | |
function wpse43054_deactivation() | |
{ | |
$role = get_role( 'subscriber' ); | |
if( $role ) $role->add_cap( 'read' ); | |
} | |
add_action( 'init', 'wpse43054_maybe_redirect' ); | |
function wpse43054_maybe_redirect() | |
{ | |
if( is_admin() && ! current_user_can( 'read' ) ) | |
{ | |
wp_redirect( home_url(), 302 ); | |
exit(); | |
} | |
} | |
add_filter( 'get_user_metadata', 'wpse43054_hijack_admin_bar', 10, 3 ); | |
function wpse43054_hijack_admin_bar( $null, $user_id, $key ) | |
{ | |
if( 'show_admin_bar_front' != $key ) return null; | |
if( ! current_user_can( 'read' ) ) return 0; | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment