• Features
  • Pricing
  • Help
  • My Account
  • Buy Now
WP Ghost
  • Features
  • Pricing
  • Help
  • My Account
  • Buy Now

Force Login Page for All Visitors | WP Ghost Guide

/Developers /Hooks /Force Login Page for All Visitors | WP Ghost Guide
Table of Contents
  • When to Force Login
  • Add the Force Login Code
  • Frequently Asked Questions
    • Does this work with WP Ghost’s custom login path?
    • Can I allow some pages to be public?
    • Does this work with WooCommerce?
    • Does WP Ghost modify WordPress core files?
  • Related Tutorials

You can force all visitors to log in before accessing any page on your WordPress site. This is useful for private sites like intranets, membership portals, client dashboards, or staging environments where no content should be publicly visible. The code below redirects all non-logged-in visitors to the login page and works with WP Ghost’s custom login path. When combined with WP Ghost’s Redirect Logged Users To Dashboard feature, already-authenticated users skip the login page entirely and go straight to the dashboard.

When to Force Login

When and why to force all visitors to the WordPress login page for private or members-only sites

Forcing login is appropriate for sites that should have no public-facing content: company intranets where only employees should access internal pages, client portals where each client sees private project data, membership or course platforms during development or invite-only phases, and staging or development environments that shouldn’t be visible to the public or search engines.

Add the Force Login Code

Add the following code to your child theme’s functions.php file or a code snippets plugin:

add_action('template_redirect', function() {
    if (!is_user_logged_in()) {
        if (isset($_SERVER['REQUEST_URI'])) {
            $url = untrailingslashit($_SERVER['REQUEST_URI']);
            $login_path = HMWP_Classes_Tools::getOption('hmwp_login_url');
            if (strpos($url, $login_path) === false) {
                wp_redirect(wp_login_url());
                exit();
            }
        } else {
            wp_redirect(site_url());
            exit();
        }
    }
});

This code runs on every page load. If the visitor is not logged in and is not already on the login page, they’re redirected to the login page. When WP Ghost’s Safe Mode or Ghost Mode is active with a custom login path, the redirect goes to the custom login URL automatically because the code uses wp_login_url().

Combine with Redirect Logged Users To Dashboard. Enable WP Ghost > Tweaks > Redirects > Redirect Logged Users To Dashboard so that already-authenticated users who visit the login page are sent straight to the admin dashboard. This gives your private site a seamless experience: visitors see the login page, logged-in users go to the dashboard, and no public content is ever exposed.

Use a child theme or code snippets plugin. Adding code to functions.php in the parent theme means your changes are lost when the theme updates. Use a child theme or a code snippets plugin to keep the code persistent.

Frequently Asked Questions

Does this work with WP Ghost’s custom login path?

Yes. The code checks for WP Ghost’s custom login URL and avoids redirecting users who are already on the login page. The redirect itself uses wp_login_url(), which returns the custom login path when WP Ghost is active.

Can I allow some pages to be public?

Yes. Add is_page() exceptions inside the code. For example, to keep the homepage and a pricing page public, add if (is_front_page() || is_page('pricing')) return; at the beginning of the function, before the login check.

Does this work with WooCommerce?

Yes, but it blocks access to all WooCommerce pages including the shop, product pages, and checkout. If you need some WooCommerce pages to remain public, add exceptions using WooCommerce conditionals like is_shop(), is_product(), or is_checkout().

Does WP Ghost modify WordPress core files?

No. The force login code is added to functions.php and uses WordPress hooks. WP Ghost itself uses rewrite rules and hooks. No core files are modified.

Related Tutorials

Login page and redirect configuration:

  • Redirect Logged Users to Dashboard – Send authenticated users straight to the dashboard.
  • Change and Hide the Login Path – Set a custom login URL.
  • Login Page Design – Customize the login page appearance.
  • Disable WP Ghost on Specific Pages – Whitelist pages from path changes.
Tagged: visitors redirectlogin redirect

Related Articles

  • WP Ghost Plugin Hooks Reference for Developers

  • Stop WP Ghost Auto Update Check | wp-config.php

  • Add a Custom CMS Simulator in WP Ghost

  • WP Ghost Constants in wp-config.php

  • Change Paths in WordPress Admin Dashboard

  • Add Files to Hide WordPress Common Files

WP Ghost 9.0

WP Ghost 9.0 is the most significant release since the plugin was renamed from Hide My WP Ghost. This update introduces a redesigned security dashboard with a real-time Security Optimization Score, a customizable login page designer, Copyright protection from AI Crawlers, interactive threat geography mapping, and more.

Wp Ghost 9

WP Ghost - Best Practice

Learn how to set up WP Ghost in Ghost Mode and activate all the security features you need for a stronger and safer website.

Most Popular

  • How to Change the admin-ajax.php Path in WordPress
  • WP Ghost Compatible Plugins and Themes List
  • What is WP Ghost?
  • Lesson 3 – Hide Your Site from WordPress Theme Detectors and Bots
  • How to Change and Hide wp-admin Path in WordPress
  • Lesson 1 – Customize Paths and Hide Your WordPress Website
  • WP Ghost Settings – Best Practice
  • Install WP Ghost Free Plugin for WordPress
  • Setup WP Ghost on Nginx Server Guide
  • WordPress Security Check – 40 Security Tasks
  • Theme Not Loading Correctly and Website Loads Slower
  • How to Configure WP Ghost Firewall Protection
  • WP Ghost Compatible WordPress Themes List
  • WP Ghost – Changelog
  • Set AllowOverride All on Apache Servers Step by Step
  • How To Change WordPress File Permissions for Security
  • WordPress Brute Force Protection with reCAPTCHA
  • How to Change the Plugins Path in WordPress
  • Change Paths in WordPress Admin Dashboard
  • Activate WordPress Debugging | WP Ghost Guide
  • Two-Factor Authentication (2FA) for WordPress
  • How to Change and Hide wp-login Path in WordPress
  • How to Change Paths in Cached Files with WP Ghost
  • How to Change the REST API Path and Disable XML-RPC
  • Disable WP Ghost In Case Of Error
  • How to Change the Registration Path in WordPress
  • How to Configure Redirects in WP Ghost
  • Hide wp-admin and wp-login.php from Source Code
  • Add Brute Force Protection to Elementor Login Forms
  • Hide WordPress from Wappalyzer | WP Ghost Guide

Product

  • What is WP Ghost?
  • Free vs Premium
  • Pricing
  • Changelog
  • Why WP Ghost
  • Knowledge Base

Features

  • Path Security
  • Firewall Security
  • Brute Force Protection
  • Two-Factor Authentication
  • User Events Log
  • Security Threats Log

Resources

  • Getting Started Guide
  • Plugin Compatibility
  • Theme Compatibility
  • Hosting Setup Guides
  • Developer Hooks
  • FAQs

Company

  • Affiliate
  • Media Kit
  • Terms Of Use
  • Privacy Policy
  • GDPR Compliance
  • Contact
  • Facebook
  • YouTube
  • X
© WP Ghost 2016-2026 | Powered by AISQ | Squirrly