Last active
December 2, 2023 12:53
-
-
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
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 | |
// 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