I’m getting this message each time I activate my plugin:
The plugin generated 80 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.
The only way I was able to suppress the message was to wrap my activation function code within an if statement (please refer to snippets below).
Here, a snippet of my plugin code when I get the error described above:
function myPlugin( $post ) {
echo "Whatever is here throws an unexpected output alert when the plugin isa activated";
}
register_activation_hook( __FILE__, 'myPlugin' );
Following, my wrapping the function in my plugin within an if statement; it suppresses the previous error as discussed above:
function myPlugin( $post ) {
global $pagenow;
if ( is_admin() && $pagenow !== 'plugins.php' ) {
echo "No more alerts when its wrapped this way";
}
}
}
register_activation_hook( __FILE__, 'myPlugin' );
What actually cause that error and how can I effectively complete my plugin with my logics without having to encounter it?
Is there any better way to handle this?
2 probably reasons:
1) You are doing an output (like
echo
or etc) in wrong place.admin
dashboard? – useadmin_notices
hook and output there…Do you want to output a message in front-end? – find appropriate places with hooks (like
the_content
orwp_footer
or whatever).Don’t output anything either in
register_activation_hook
or outside of WordPress standard hooks, no-one should do that.**2) if you aren’t doing any output intentionally, then maybe some
php
error happens? If so, put this code temporarily infunctions.php
and then activate the plugin – you will see the error.Had the same error, but only with 6 characters )
so… in my case I had empty lines after PHP closing tag ?> – that will cause this error too.
I think there may be two issues here that are causing the problem. First is that I don’t think wordpress expects any output when the plugin activation hook is called so it may be complaining about that. Second is that plugin activation hooks are called fairly early in the wordpress program flow, so, it’s probably being called before headers are sent. If ANY output is generated before calling
header()
then PHP usually complains.Usually the plugin activation routine is reserved for basic setup of the plugin, calls to things like
set_option()
and the like.I had the same error – 3 characters of unexpected output and was lead here. For people in my scenario another cause of this message can be the file type being encoded as UTF with BOM.
BOM encoding was causing the error, and while the plug-in activated it would render incorrectly in internet explorer because of this.
The solution is to use Notepad++ and choose ‘Convert to UTF without BOM’, or if you are using visual studio, there is an explanation of how to change encoding UTF-8 without BOM
I battled this problem for a long time. Typically this is caused by spaces or new lines before the opening
<?php
tag or after the closing?>
tag. Once I removed these, the error went away.Also, never assume anything about
GET
,POST
,COOKIE
andREQUEST
variables. Always check first usingisset()
orempty()
.In my case it was due to Undefined index, Just enable the debug log to check whats causing it, then you can resolve it easily.
For those who don’t know how to enable the Debug log, Add these lines in your wp-config.php:
You can see the errors properly in the debug file created in wp-content
sometime it is because you use
<?php ;?>
unnecessary or use it like shown belowthis extra line between closing and starting tag may also cause this error, simple remove that line/space
A common way to assign the register_activation_hook is using a static method of a class. This ensures that your plugin activation function name won’t collide with other plugins.
This function needs to be public and not private. A mistake is easily made though, so this could be a reason for your problems when getting this kind of error.
You would then register the activation with this code in the main plugin file.
The error message
The plugin generated *X* characters of unexpected output during activation
is not very helpful or at least not quite enough.To help locate the issue, add these to
wp-config.php
:Then check
wp-content/debug.log
for more detailed error messages as to the origin of the error.I had the same problem. I noticed that there were new lines at the beginning of the file. So I removed them and the errors disappeared. Try removing the new lines at the beginning of your files. That may help.
For beginner level developer its must be empty line break after “?>” closing of php tags.
Try removing all empty line break after this.