• Hello,
    I created a page in wp-admin>New page:
    https://www.example.com/new-page-custom-user

    There is also a role: seller
    I do not want users without this role to access this page. For this, I took advantage of this resource and wrote the following code.
    However, I couldn’t get what I wanted. The code is not working. So where am I doing wrong?

    My Code:

    add_action( 'template_redirect', 'block_access' );
    function block_access() {
        if ( is_user_logged_in() && is_page( 'new-page-custom-user' ) ) {
            $user = wp_get_current_user();
            $valid_roles = [ 'administrator', 'seller' ];
            $the_roles = array_intersect( $valid_roles, $user->roles );
    
            // The current user does not have any of the 'valid' roles.
            if ( empty( $the_roles ) ) {
                wp_redirect(home_url());
                exit;
            }
        }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Role-Based Page Access’ is closed to new replies.