How to redirect all non-logged in users to the default wordpress login page and bypass some pages using it's slug ID?

  • You can redirect users to login page with wp_safe_redirect( wp_login_url() ); function. but I don't understand your question exactly. – Mostafa Soufi Sep 3 '17 at 9:51
  • What I wish to achieve is to block the full WP and redirect to wp-login but bypass some pages which even a non-loggedin user can view. I hope I'm more clear now. – HDR Sep 3 '17 at 10:33

Something like this should do it:

add_action('wp', 'custom_user_redirect');
function custom_user_redirect() {
    if (is_user_logged_in()) {return;}
    // array of page IDs OR slugs to allow access to
    $skippages = array(11, 23, 12); 
    foreach ($pages as $page) {
        if (is_page($page)) {return;}
| improve this answer | |
  • Is there a specific reason you're adding to wp (which is the first time I've read this hook) instead of init or template_redirect? – kero Sep 4 '17 at 7:53
  • not really I was just in a rush and didn't think about the hook too much, either of those would work too – majick Sep 5 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.