I am trying to redirect this page if the user is logged in or not be i keep getting this error

Warning: Cannot modify header information - headers already sent by (output started at /home/*******/*********.com/wp-content/plugins/graph2.php:54) in /home/*******/*********.com/wp-includes/pluggable.php on line 934

Im using this redirect method

<?php if ( is_user_logged_in() ) { 
        echo ''; 
        else { 
    wp_redirect( 'http://www.example.com' );    

I cant seem to figure out what is wrong.

:) thanks

  • In which file do you have that redirect code? Within the theme function.php or a plugin? – feeela Jul 20 '11 at 20:57
  • neither i was putting it in the header.php in my template folder – user705260 Jul 20 '11 at 21:00
  • "headers already sent" just means, that some output was already sent to the client. Try moving it to the functions.php and see if it works. Your redirect code must be executed before any output was sent. See also How to change plugins load order? – feeela Jul 20 '11 at 21:05

Replace wp_redirect( 'http://www.example.com' ); with wp_redirect( 'http://www.example.com' ); exit;

| improve this answer | |
  • I would down vote this if I had enough rep. This is not a useful or relevant answer to the question being asked. – SplinterReality Apr 20 '12 at 5:15
  • 1
    I probably should have explained that better. The "exit;" is very important for getting rid of the headers already sent message. That's the only problem with his code. – Pippin Apr 20 '12 at 18:37

The problem is most likely from whitespaces in the graph2.php line 54. Delete the white spaces which might be after the closing ?> php tag, that should solve the problem.

Its best practice to follow wp_redirect( 'http://www.example.com' ); with exit();

| improve this answer | |

you might have to delete <?php get_header(); ?> from the top of your file and make your code on the very 1st line of your file

| improve this answer | |

This is the result of a poorly coded plugin I'm afraid. If you know PHP well enough, you might try modifying the wp-content/plugins/graph2.php file that is mentioned, and make sure that the first line starts with <?php (no spaces or blank lines) and that the file does not output anything else when it's first loaded. You should exclude the closing ?> tag in the file as well, just to be on the safe side. (Yes, this is legal PHP)

You'll also likely find that your page output and RSS feed has blank lines or extra content at the top of it, and RSS will be broken in some versions of IE as a result.

Plugin quality REALLY VARIES from author to author, and it takes some effort to screen the quality of the plugins you use, but this unfortunate case shows why that really is necessary.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy