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]
- is_user_logged_in() doesn’t work after custom login and redirect
- How to detect mobile devices and present them a specific theme?
- Using wp_add_inline_style without a stylesheet
- The difference between calling wp_enqueue_scripts to load scripts and styles in custom theme
- WordPress twentytwenty theme inline css overriding css body
- How can I insert default widgets when my theme is activated (similar to what twenty eleven does)?
- Featured Image in 4.4 should be larger on mobile device
- How to disable the gradient and font size internal CSS added by WordPress when using theme.json
- Redirect user after login/registration globally
- How can you determine whether an image is merely attached or has actually been inserted into a post?
- How do I display the “Archives” widget layout (sidebar) in WordPress by ‘year’ then by ‘months’?
- Where is the correct place(s) in twentysixteen’s header.php to add a Google webfont?
- Flexible WordPress theme creation using themes
- How can one update a theme on a live site without interruption?
- How to modify the Register link in the login page?
- Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
- How to move the the_excerpt’s read more link to another div
- How to create a custom template to admin dashboard
- Change The Meta-Description Of Home-Page?
- How to redirect on login to a specific page if a specific meta user is empty
- Add, edit specific CPT with custom role
- how to display search term in the template full site editor
- How to add new page to wordpress theme through coding?
- Dynamically generated wordpress customizer options?
- Custom Blocks as part of a theme
- How to set up diffrent mobile theme for single site in WordPress?
- Derive child theme from separate theme
- How to change footer or for different kinds of users in wordpress?
- How to Download Minimum Requirement of WordPress (Not Themes )
- Create a child theme from multiple themes
- How can I use the WordPress Loop and Pagination in multiple instances but different scenarios throughout my site?
- How to fix : Uncaught ArgumentCountError: Too few arguments to function? [closed]
- I have WordPress setup on primary domain, how do I set it up on its subdomain using the same database?
- Theme development – Automatically menu creation
- pre_get_comments or the_content filter
- using part of a theme in another theme
- What might cause a featured image not to display?
- Remove/Hide a Widget on a Custom Template
- Creating an advanced searchform based on select boxes
- Setting thumbnail featured image size not working properly
- Why WordPress developer still using timthumb and what are advantages using them? [duplicate]
- finding menu hierachy > wp_get_nav_menu_items
- how to display recent post by category on home page
- Show popover with url and option to open in new window
- Custom pages with html in wordpress editor vs. writing it all directly in the php template files?
- Menu not updating for logged in users after redirect
- Show all menu levels except top level. How to exclude top level of a menu?
- WooCommerce – edit templates
- Redirect user on login using extra field value on login form
- How to dowload and edit content of a website
- Uncaught Reference error in Retina.js
- Replaceing _custom_background_cb
- Best strategy for providing theme options using customizer
- Theme Development for a Newbie
- How can I fix the even/odd child classes comments so that the comments are unaffected by the top-level even/odd?
- Deleting all Options on theme switch
- Is there any open source WordPress Themes?
- DIV containing iframe disappears below break point of 992 in Oxygen Builder
- After a while attachment meta data is missing “sizes” data
- Template for posts of category in block theme?