Site logo

Archived topic

Cannot modify header information error with hook to check is_user_logged_in()

3 replies · Started by Jessica on August 21, 2022

Viewing posts 1–4 of 4

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!

You’re welcome Jessica! Glad you found a solution!

This archived topic is closed to new replies.