As Milo stated, you can’t send header information after PHP has begun sending request response body.
Redirects belong in a function hooked into wp_loaded
to ensure they run before the request body is generated.
You need something like the following in your functions.php
file:
function hook_wp_loaded_require_login() {
$uri = ( isset( $_SERVER['REQUEST_URI'] ) )
? $_SERVER['REQUEST_URI']
: null;
if (
$uri &&
$uri !== '/login/' &&
$uri !== '/policy-agreement/' &&
! is_user_logged_in()
) {
$url = get_site_url() . "/login/";
wp_redirect($url);
exit;
}
}
add_action( 'wp_loaded', 'hook_wp_loaded_require_login' );
Note: If you use this approach, users will never see your You must be logged in to see the team details.
error message and you’ll also need to follow up on that policy_agreement_redirect()
call…
Related Posts:
- Error messages when adding code to function.php or trying to delete inactive plugin files
- Getting headers already sent error from plugin
- Call to undefined function get_header() in index.php
- White Screen on Site but wp-admin OK
- Fatal Error: get_header();
- Multiple use wp_logout_url() on page cause ‘header already sent’ error after submit form
- Fatal error: Uncaught Error: Call to undefined function add_action()
- Why I am having error Call to undefined function get_header()?
- Error Message – Cannot login to WordPress
- How to disable the fatal error (WSOD) protection?
- Could not create directory /wp-content/upgrade/
- Getting the warning: Missing argument 2 for manage_posts_custom_column()?
- What log files does a WordPress installation write to?
- HTTPS leads to Sorry, you are not allowed to access this page
- What is the best way to monitor PHP functions/executions?
- How to use WP_Error $data argument?
- Is it possible to disable caching of an option when using w3 total cache?
- Showing error “Function create_function() is deprecated”
- error_log over 70GB. How to stop this? [closed]
- Update wordpress fail – There has been a critical error on your website
- “Notice: Undefined index:” error when adding new content?
- Object of class WP_Error could not be converted to string
- New WordPress Error Message – Not seen this before
- Headers already sent – WordPress core
- Recommend a guide to catching plugin errors, please?
- Getting Warnings & Notices from Fresh WordPress 3.1.2 install
- Perform a redirect after user action
- Cannot modify header information – headers already sent by pluggable.php
- What do these “Failed to import” errors mean and how do I fix it?
- How to use add_settings_error in register_setting callback
- Application passwords not working on localhost?
- WordPress home page doesn’t work, but other pages do. How to rectify?
- header and wp_redirect not working. cannot modify header information warning
- Only on 404 page, get notice: trying to get property of non-object on nonce
- In big trouble – Allowed memory size of 268435456 bytes exhausted
- failed to open stream: No such file or directory
- update user information
- Error! 401.shtml file not found, what’s the problem?
- Strange glyphs code error
- Sorry, you are not allowed to access this page dashboard
- Custom slider throwing an error
- err_connection_closed after 20 minutes when loggedin in backend
- Homepage Not Found Error [closed]
- 4.2.2 stuck in an db upgrade loop with “Call to undefined method wpdb::get_col_length()”
- Cannot redeclare maybe_create_table()
- Fatal error: Allowed memory size of 146800640 bytes exhausted (tried to allocate 57 bytes) in public_html/wp-includes/rewrite.php on line 1561
- Display all WP Errors together during form validation
- Updating posts exceeds maximum execution time
- Front end empty (i.e. nothing displays); Back end looks fine; What are possible causes?
- Website Warning: call_user_func_array() expects parameter 1 to be a valid callback, class ‘Wppr_Public’ does not have a method ‘amp_support’
- Debugging: How to determine which plugin is throwing error in plugin.php
- How can I use wordpress functionality outside of WP framework
- Errors in Add Image Popup, Media Library Tab: “in_array() expects parameter 2 to be array, null given in wp-includes/query.php”
- hypanis.ru wordpress vulnerabilty [closed]
- Child Theme Fatal Error [closed]
- Jetpack could not contact wordpress [closed]
- Is this a correct usage of ob_start() in my WordPress project?
- Dummy data error
- W3 Total Cache “Preview mode could not be disabled” [closed]
- Problem with UTF8. italian/russian characters
- Failed to import “File name”: Invalid post type jetpack-portfolio
- Error call_user_func_array() After Installing Plugin
- Error 500 when installing WordPress
- Why I get a blank screen when trying to make wordpress post/pages edits?
- Unable to edit with Elementor
- Warning: call_user_func_array()
- Getting Download failed. cURL error 28: Connection timed out after 10001 milliseconds
- “File not found.” appears often while navigating wp-admin
- WordPress Error blank page In section website
- Posts, pages and some images are deleted sometimes
- Call to undefined function wordpress listing_categories
- how do i fix these errors my debug log gave me?
- Cannot modify header information – headers already sent during plugin activation
- Undefined index when saved to options
- Fatal error: Call to undefined function wp_add_privacy_policy_content() in /wp-admin/includes/misc.php on line 1822
- Unable to customize themes in wordpress after migrating to https from http
- A SINGLE Post Giving an “Under Maintenance” Message
- Weird Cron Error
- Headers Already Sent
- Overcome illegal characters [duplicate]
- How to find which plugin crashes
- Undefined Index. But isset not working?
- bypassing permission error for a plugin’s page
- Website Loading Very Slow, Each Time Different File Takes Very Long To Load
- Fatal error: Allowed memory size of bytes exhausted (tried to allocate 36 bytes)
- Logging out gives 404 error
- Any idea on how to fix this error when forcing SSL on a certain page?
- Internal 500 error: Activating rootio theme and deploying with capistrano
- White background, black text – the simplest on earth : “error”!
- Twenty Eleven theme-options error
- warnings & errors after MAMP to live (hosteurope)
- I’m getting Call to undefined function wp_parse_list() out of the blue
- What are some best practices to clean up http mix content warnings?
- Fatal error due to file permissions
- undefined index [closed]
- WordPress is not sending the 2nd email in the same request
- Fatal error: Uncaught Error: trim(): Argument #1 ($string) at \wp-includes\class-wp-query.php
- Unable to log in: “Cannot serve directory www/wordpress/wp-includes/ No matching DirectoryIndex”
- wp_redirect problem using code snippets
- How do I turn off WordPress errors and just have regular old php errors?