You can use the wp_loaded
hook to push all non-logged-in traffic to the login screen. Note how I’ve passed $_SERVER['REQUEST_URI']
to wp_login_url()
– this is passed as a URL parameter to the login page, and once a user successfully signs in, they’ll be redirected back to the URL they were originally trying to visit.
function wpse_178177_redirect_to_login_if_guest() {
if ( ! is_admin() && ! is_user_logged_in() && $GLOBALS['pagenow'] !== 'wp-login.php' ) {
wp_redirect( wp_login_url( $_SERVER['REQUEST_URI'] ) );
exit;
}
}
add_action( 'wp_loaded', 'wpse_178177_redirect_to_login_if_guest' );
Related Posts:
- When to use is_home() vs is_front_page()?
- Enqueue a stylesheet for login page and make it appear in head element
- How to use logout function on custom menu link?
- redirect does not work in ajax function
- How to customize wordpress login/register pages?
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Set front page as static page [closed]
- How do I create a specific login page for a specific user?
- Adding “latest from the blog” to the homepage
- how can I remove the sidebar from my wp homepage [closed]
- current_user_can Not Always Working Properly
- WordPress Theme Development: How to redirect templates the right way?
- How to make Home Menu( Front Page) use index.php instead of page.php
- Redirect Old .php URLs to New WordPress Page
- Redirect to specified page when insufficient permissions?
- Can’t login unless twentyfifteen theme is active
- Where to find the code that redirects requests on the static page URL to the home page
- Showing a list of posts when homepage is custom
- How can I force the user to log in, even if they’re already authenticated?
- How to remove text “Log In” from login page
- How can I conditionally show different home page templates based on whether or not the user is logged in?
- I used single quote to design HTML of my WordPress blog. Now site redirects to index.php everytime than page.php or single.php
- Custom Login Form – Redirect user to login page if not logged in
- Theme Development -> Specific Homepage
- Disable redirect canonical
- Exclude function from homepage only?
- 1 post per category on homepage and counting those posts
- Janrain/Simple Modal under Redirected Domain
- Cart displayed above header
- Custom Static Page to Display Blog Posts in Excerpt Non-Singular Form (is_singular not working?)
- redirect_to how to make it simply work with get parameter or similar?
- Developing WordPress site behind a static site
- annoying “Confirm Form Resubmission” message
- What is the best practice workflow for new website overhaul and keeping current SEO ranking? [closed]
- Check if redirected from a specific page template
- 3 x 3 grid of posts on the home page
- How can I change the title of the Home link in the navigation bar?
- Allow users to edit home page from WordPress (home.php problems)
- What is the advantage of using home.php over index.php for the front page
- How can I enforce user to use Application password to generate JWT token? [closed]
- How important is it to enqueue a theme’s stylesheet?
- How do I remove a pre-existing customizer setting?
- Adding extra authentication field in login page
- Why I can’t add a CSS style in this WordPress theme?
- Need to execute a cron job
- Masking logout URL
- Posts in multiple Categories different single.php
- Can you filter posts by meta key with the getEntitityRecords selector?
- add_theme_support( ‘custom-header’ ) does not add option menu in dashboard
- how to make a child theme replace functions in different php files that are loaded throu require_once
- How WordPress converts URL to $query_string
- 2 differen’t menus and it’s using same items in both
- PHP If user is logged in & on home page redirect
- “add_theme_support” for custom footer color?
- Media Uploader in custom path
- Highlight “Show all” item in wp_list_categories
- Attach media to multiple posts
- Internal error 500 accessing Theme > Customize
- Theme Development: Starter Content after hook
- Custom page template with custom fields
- Underscore Based Theme File Permissions in Git
- Two factor authentication
- HTML added to editor when deleting shortcodes in Chrome browser
- Widget Option is Missing
- How do I use a color from theme options?
- How to include a file only on dashboard widgets page?
- WP_Query $gallerypage->the_post() giving Undefined offset: 0
- Redirect /home to home.php
- How do I conditionally defer scripts based on the page name?
- How to place POST TITLE any location inside the posts page
- Do I need to re-import Susy to work with it in a child theme? [closed]
- Valid SQL query not returning results
- Change Image Sizes for Mobile Theme
- Recommended sidebar / content widths
- Trouble in creating a custom template on twentyeleven theme
- language support in a custom urdu theme
- Understrap-child conditional js script
- In wordpress customizer div still showing even if I hid it with javascript
- Programatically Created Menu not Editable in Dashboard
- Embedding CodePen Calorie Script – jquery error
- Custom user roles are unable to login
- How to make updatable custom WordPress theme?
- How can I access variables from theme in child theme? [closed]
- Is it possible to echo a woocommerce prouct attribute discription?
- change background image on scroll
- Duplicate child theme incl. design changes
- Why .widget-area is outside of .site-content in Underscores starter theme?
- Using wp_add_inline_style Inside a Template File
- stop customize.php redirect to login page if admin is not logged in
- WordPress Twenty-Fourteen: How to Remove Home Page Header Image from Other Pages
- Modify comments_popup_link output
- Create image with imagepng() (fails at header)
- Can you create a custom page with content in the center already defined?
- WordPress redirect to splash page once a day for the first week
- RSS feed url showing page not found. How to solve it?
- Enable WP to accept standard jQuery code embeding between the tags [duplicate]
- Retina ready for uploaded images which are cropped
- Display Page featured Image as well as Posts featured Image
- Creating completely new WordPress site from old WP contents
- Configuring static page with add_rewrite_rule gives 404 after navigating to Permalinks admin panel