• Resolved NAROLLES

    (@narolles)


    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 answer

    Yes, 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 $whitelist with $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:

    1. Fixed the $whitelist variable → Replaced it with $allowed_roles.
    2. 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]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Do not apply to administrators’ is closed to new replies.