2

I am getting the following error when I activate my plugin:

The plugin generated 22 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

And here's my plugin code:

<?php
/*
Plugin Name: Hello World
*/

    echo "<h1>Hello, world!</h1>";

?>

I know we get this error usually when there's some syntax error in the code. I have checked, there are no blank spaces, \n, \t, \r or any kind of special characters at the starting or ending of the file.

I don't understand, this is the most minimalist plugin one could ever create, what's wrong?


EDIT 1:

I don't get the error if I remove the echo "<h1>Hello, world!</h1>"; line.

3
  • Have you checked inside your debug.log? Aug 17, 2018 at 12:40
  • @Ben I solved it as per the answer given by fuxia. But would you be kind enough to let me know where can I find the debug.log file? Aug 17, 2018 at 12:45
  • 2
    Here: WordPress Debug Log
    – Fayaz
    Aug 17, 2018 at 12:59

1 Answer 1

4

Plugins are loaded before headers are sent. The reason is that you should be able to send your own headers per plugin.

And that's why a main plugin file must not create direct output. Wrap your echo code into a function and register that function as a callback for an action that happens later, after the headers are sent.

1
  • Ohh, got it. Sorry, I'm new.. Solved it, and it worked, thanks! I blame the tutorial which I am following.. Aug 17, 2018 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.