Do not apply to administrators
-
Hi
I wanted to apply the plugin except to administrators (it would be good perhaps natively with a checkbox)
I have via snippets inserted your recommended code on the pages of the public interface
and I have a fatal error
via chatgpt I asked about the code, here is his answerYes, there is a coding error in your function. You are using an undefined variable:
$whitelist. However, in your code, the list of allowed roles is stored in the variable$allowed_roles.Correction:You should replace
$whitelistwith$allowed_roles.Here is the corrected code:
function loggedin_bypass_roles( $prevent, $user_id ) { // Array of roles to bypass. $allowed_roles = array( 'administrator' ); $user = get_user_by( 'id', $user_id ); $roles = ! empty( $user->roles ) ? $user->roles : array(); return ! empty( array_intersect( $roles, $allowed_roles ) ); } add_filter( 'loggedin_bypass', 'loggedin_bypass_roles', 10, 2 );Explanation of Fixes:
- Fixed the
$whitelistvariable → Replaced it with$allowed_roles. - Logical structure remains intact → The role intersection logic works correctly after this fix.
With this modification, your code should work as expected.
The page I need help with: [log in to see the link]
- Fixed the
The topic ‘Do not apply to administrators’ is closed to new replies.