Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lunule/956978377db70277b9a78da2b65acde7 to your computer and use it in GitHub Desktop.
Save lunule/956978377db70277b9a78da2b65acde7 to your computer and use it in GitHub Desktop.
[WordPress - Access restriction without plugins] #access #restrict #wp #core #collection #bestof #redirect #roles
<?php
// Use the $pagenow global.
if( is_admin() ) {
add_filter( 'init', 'custom_restrict_pages_from_admin' );
}
function custom_restrict_pages_from_admin() {
global $pagenow;
$arr = array(
'update-core.php',
'edit.php'
);
if( custom_check_user_roles() && in_array( $pagenow , $arr ) ) {
//echo some custom messages or call the functions
} else {
echo 'This page has restricted access to specific roles';
wp_die();
}
}
function custom_check_user_roles() {
//restrict pages by user role
return (
current_user_can( 'administrator' ) ||
current_user_can( 'editor' )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment