phpwordpress

The plugin generated X characters of unexpected output during activation (WordPress)


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?


Solution

  • 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.