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

Home Forums Support [Resolved] Cannot modify header information error with hook to check is_user_logged_in()

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2319314
    Jessica

    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!

    #2319495
    Fernando
    Customer Support

    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/

    #2321562
    Jessica

    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

    #2321785
    Fernando
    Customer Support

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.