Skip to content
Be-smart

Be-smart

  • Home
  • Blog
    • Most Visited Posts
    • Stories
    • Unisex
      • Kids
      • Man
      • Woman
    • ADS
    • Social Media
  • About
    • Contact
  • Login

How to redirect users who are not logged wordpress function

Photo of author
Flora
11/01/2025
Views: 31

Estimated Reading Time: 2 min

To redirect users who are not logged in to a specific page in WordPress, you can add the following code to your theme’s functions.php file:


Redirect Non-Logged-In Users



function redirect_non_logged_in_users() {
    // Check if the user is not logged in and not on the login or registration page
    if (!is_user_logged_in() && !is_page(array('login', 'register'))) {
        // Redirect to the login page or another specified page
        wp_redirect(home_url('/wp-login.php')); // Change '/wp-login.php' to your desired URL
        exit; // Terminate further execution
    }
}
add_action('template_redirect', 'redirect_non_logged_in_users');


  

How It Works

  1. is_user_logged_in(): Checks if the user is logged in.
  2. is_page(): Excludes specific pages from the redirection (like the login or registration page).
  3. wp_redirect(): Redirects users to the specified page (e.g., /wp-login.php).
  4. exit: Stops further processing after the redirection.

Customization

Change Redirect URL: Modify the URL in the home_url('/wp-login.php') to point to your login or any other page.

Exclude Additional Pages: Add more page slugs to the is_page() array:



if (!is_user_logged_in() && !is_page(array('login', 'register', 'about', 'contact'))) {
    wp_redirect(home_url('/wp-login.php'));
    exit;
}



  

Exclude Admin Area: To ensure non-logged-in users are redirected only for frontend pages, check:



if (!is_user_logged_in() && !is_admin() && !is_page(array('login', 'register'))) {
    wp_redirect(home_url('/wp-login.php'));
    exit;
}



  

Notes

  • Ensure the redirect URL (/wp-login.php) is publicly accessible to non-logged-in users. This could be the WordPress default login page or a custom page.
  • If you’re using a custom login plugin or membership system, make sure to align the URL and conditions accordingly.

Let me know if you need help adapting this further!

Categories BSB, Html, WordPress
The Best Foods for Heart Health | Part two
Here are ten highly regarded restaurants in Brasília, Brazil
Share on Facebook Share on WhatsApp Share on X (Twitter) Share via Email

Related Posts

  • How to optimize robots.txt for a beginner WordPress admin
  • The winner of the title Miss Italian Mother 2024
  • What are the potential long-term effects of untreated infections in infants?
  • Images you’ve never seen before 100% guaranteed
Photo of author

Flora

How to redirect users who are not logged wordpress function

Published 11/01/2025

Update 11/01/2025

Contact

  • X
  • TikTok
  • Facebook
  • WhatsApp
I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.

Bitcoin Price (BTC)

$104,835.72

Download BTC Ticker

Trending Posts

  • Here are ten of the most iconic and celebrated BMW models of all time
  • BMW M4 Competition (F82): Redefining Performance and Style
  • How to Clear Cache BlueHost with Cron Job Command
  • Legendary ‘Killing Me Softly’ singer, dead at 88
  • Euro 2024 Qualification Groups: All You Need to Know

Recent Posts

  • Fresh and Healthy Heart of Palm Salad Recipe
  • Ultimate WordPress Countdown Timer: PHP, CSS & JavaScript Guide
  • How to create a related posts section
  • How to uninstall Windows programs using a batch script
  • Here are 5 cool WhatsApp tricks you might not know about:

Archives

  • March 2025 (1)
  • February 2025 (65)
  • January 2025 (94)
  • December 2024 (42)
  • November 2024 (69)
  • October 2024 (92)
  • September 2024 (31)

Recent Comments

No comments to show.

Partners Group

Amazon AliExpress BlueHost
GeneratePress SnepBelgium Takeaway

BlogRoll

Mirror

The MixedZone

Finance Digest

  • Facebook
  • X
  • WhatsApp
  • RSS Feed
  • Facebook
  • X
  • WhatsApp
  • RSS Feed

BSB (228) Celebrity (24) Europe (37) Food (14) Html (41) Lifestyle (23) Love (18) Medical (15) Movies (18) My Health (28) PC World (36) PHP (22) Science (15) Social Media (13) Software (14) Stories (96) Weekly Horoscopes (13) Windows (29) Woman (20) WordPress (76)

  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Cookie Policy

© 2024 Be-smart

All Rights Reserved

Built with GeneratePress

This website uses cookies to ensure you get the best experience. By continuing to browse, you agree to our Privacy Policy.