The plugin generated X characters of unexpected ou

2018-12-31 14:01发布

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?

标签: php wordpress
15条回答
步步皆殇っ
2楼-- · 2018-12-31 14:38

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.

查看更多
听够珍惜
3楼-- · 2018-12-31 14:39

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.

查看更多
与风俱净
4楼-- · 2018-12-31 14:39

i also got this problem when i activate my plugin

The plugin generated 1 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.

Typically this is caused by spaces or new lines before the opening tag. Once I removed these, the error went away.

now my plugin error gone.

查看更多
登录 后发表回答