Archived topic
Cannot modify header information error with hook to check is_user_logged_in()
3 replies · Started by Jessica on August 21, 2022
Hi,
I wanted to check if the user is logged into WordPress before showing a page. If user is logged in, redirect to page A, if not, redirect to page B.
Added a hook to wp_head with the following PHP code but I got an error that the header information cannot be modified...
<?php
if (is_user_logged_in()) {
header("Location: http://www.youtube.com");
} else {
header("Location: http://www.google.com");
}
?>
Got the following error code:
Warning: Cannot modify header information - headers already sent by (output started at /homepages/XXX/wp-content/themes/generatepress/header.php:12) in /homepages/XXX/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 5
Any help to get over this error is greatly appreciated!
Thanks!
Hello Jessica,
Sorry, but this isn’t something a theme (any themes) would handle. I found this thread which may help: https://stackoverflow.com/questions/25918168/wordpress-redirect-when-user-is-logged-in-on-login-page#:~:text=All-,answer,-here%20did%20not
It would also be good to reach out to WordPress’ support team with regards to this: https://wordpress.stackexchange.com/
Thanks a lot for your insights.
I have resolved my problem with the help of this page. Hope this may help others who are expericing the same issue:
https://stackoverflow.com/questions/65396235/require-wordpress-login-to-view-page
You’re welcome Jessica! Glad you found a solution!