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 to make checkbox required
- is_user_logged_in() doesn’t work after custom login and redirect
- Menu items description? Custom Walker for wp_nav_menu()
- How to define and link full path to css located at a random folder on header.php
- Sizing screenshot.png without losing aspect ratio
- Software for WordPress Theme and Plugin Development? [closed]
- What is the preferred way to add custom javascript files to the site?
- Get the blog page URL set in Options
- How to create .pot files with POedit?
- Can I programmatically login a user without a password?
- How do I get the theme URL in PHP?
- Why when I submit a form in wordpress it loads a 404 page though URL is correct
- What is the constant WP_USE_THEMES for?
- Worthwhile to restrict direct access of theme files?
- How Do I Protect My Premium WordPress App Theme from Copying?
- How to detect mobile devices and present them a specific theme?
- Using wp_add_inline_style without a stylesheet
- How to add posts to custom menus?
- WordPress Theme Preview Image
- How To extend WP_Customize_Control
- What is the difference between front-page.php and home.php? [duplicate]
- How can I get page slug
- Does the functions.php file ever get called during an AJAX call? Debug AJAX
- When to use _e and __ for the translation?
- What is a Theme textdomain?
- How to show a custom meta box on the “Quick Edit” screen?
- How to add CSS class to custom logo?
- How to add custom css file in theme?
- How important is it to enqueue a theme’s stylesheet?
- CSS not updating in browser when I change it
- Display Search Result Count
- wp_nav_menu(), how to change class?
- theme path in javascript file
- Is wp_is_mobile() effective?
- Redirect user to original url after login?
- Display Menu Name using wp_nav_menu
- Making update notification functionality for my themes
- What questions do you ask when asked to give an estimate on theme development?
- How to remove dashicons.min.css from frontend?
- How do I remove a pre-existing customizer setting?
- Minimum Template Files for Theme Development
- What’s the difference between home.php and index.php?
- pass object/JSON to wp_localize_script
- get_template_part vs action hooks in themes
- Theme Activate Hook
- Solutions for generating dynamic javascript / CSS
- wp_redirect() – headers already sent
- Can I create my own “Recent Posts” widget or customize the existing one?
- Custom single template for a specific category
- how can I add an icon/image for a child theme?
- How can I add a URL field to the attachments window?
- The the_post_thumbnail without srcset?
- The proper way to include/require PHP files in WordPress
- Get the first image from post content (eg.: hotlinked images)
- after_setup_theme always runs
- When to use add_action(‘init’) vs add_action(‘wp_enqueue_scripts’)
- Getting failure when using filemtime() with wp_enqueue_style
- Do I actually need to link my theme’s style.css in the theme files
- Why is wp_head() creating a top margin at the top of my theme header?
- How to store widget fields data as an array?