Custom Login page, redirection and restrictions

  php, wordpress

I’m having some issues with my custom login page with redirections and restrictions. Any user can browse a ‘community’ page but they can’t access the ‘distributors’ pages. I have set a rule so if anyone that it’s not logged in will be forwarded to the home page. Those who are logged will be redirected to the ‘distributor’ pages. Everything seems to be working fine except on Firefox. For the custom login page I’m using a form from Elementor.

Here’s custom code I have to restrict non-logged users to some pages.

/*
*   Restrict non logged users to certain pages
*/
add_action('template_redirect','my_non_logged_redirect', 10, 2);
function my_non_logged_redirect()
{
    if ((is_page('distributors') || is_page('distributor-success-stories') || is_page('distributor-solutions') || is_page('distributor-software') || is_page('distributor-product-software') || is_page('product-smart-projectors') || is_page('distributor-product') || is_page('product-accessories') || is_page('distributor-press') || is_page('distributor-media') || is_page('distributor-firmware')) && !is_user_logged_in())
    {
        wp_redirect( '/login' );
        die();
    }
}

Here’s the code I have to redirect registered users after they successfully log in

/**
 * Redirect non-admins to the homepage after logging into the site.
 */
function custom_login_redirect( $redirect_to, $request, $user  ) {
    return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : '/distributors';
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

After a successful login, the user is being redirected to the login page again where it should be redirected to the /distributor page.
It looks like the rule to restrict logged out users to the /distributors page don’t recognize that user is logged in to grant access.

Can anyone help me with it?

Source: Ask PHP

LEAVE A COMMENT