Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top