I am working on a site where new users are expected to agree to read our site policy before they can proceed. Once a new user logs in, they are redirected to the policy page. To continue to the read of the site, they need to check an "I agree" box.
My problem is that the users are no longer being directed to the policy page anymore, the redirect fails. This is the error message I am getting:
PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/class.wp-styles.php:225) in /var/www/html/wp-includes/pluggable.php on line 1226
PHP Stack trace:
PHP 1. {main}() /var/www/html/index.php:0
PHP 2. require() /var/www/html/index.php:17
PHP 3. require_once() /var/www/html/wp-blog-header.php:19
PHP 4. include() /var/www/html/wp-includes/template-loader.php:74
PHP 5. policy_redirect() /var/www/html/wp-content/themes/mytheme/landing-page.php:68
PHP 6. wp_redirect() /var/www/html/wp-content/themes/mytheme/functions.php:691
PHP 7. header() /var/www/html/wp-includes/pluggable.php:1226
I'm not really sure what parts of my code are relevant to post here, I haven't touched class.wp-styles.php or pluggable.php.
Here is the redirect function in functions.php:
function policy_agreement_redirect(){
$url = get_site_url() . "/policy-agreement/";
wp_redirect($url); //THIS IS LINE 691 WHERE THINGS BREAK
exit;
}
Every page on my site contains something to redirect users in case they are trying to go to a page but haven't agreed to the policy yet. For example, here's what the relevant part of landing-page.php looks like:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<div class="entry-content entry">
<?php the_content(); ?>
<?php if ( !is_user_logged_in() ) : ?>
<?php
$url = get_site_url() . "/login/";
wp_redirect($url);
exit;
?>
<p class="warning">
<?php _e('You must be logged in to see the team details.', 'profile'); ?>
</p>
<?php endif; ?>
<?php if( !check_policy_agreement() ): ?>
<?php policy_agreement_redirect(); ?>
<?php else : ?>
<?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
<!-- THE REST OF THE BLOG -->
Any ideas on what is causing this error and what I should try? Any help is appreciated. Thank you!