Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 379
Scott B Avatar asked Nov 02 '10 02:11

Scott B


2 Answers

2 probably reasons:

1) You are doing an output (like echo or etc) in wrong place.

  • Do you want to output a message in admin dashboard? - use admin_notices hook and output there...
  • Do you want to output a message in front-end? - find appropriate places with hooks (like the_content or wp_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 in functions.php and then activate the plugin - you will see the error.

define('temp_file', ABSPATH.'/_temp_out.txt' );  add_action("activated_plugin", "activation_handler1"); function activation_handler1(){     $cont = ob_get_contents();     if(!empty($cont)) file_put_contents(temp_file, $cont ); }  add_action( "pre_current_active_plugins", "pre_output1" ); function pre_output1($action){     if(is_admin() && file_exists(temp_file))     {         $cont= file_get_contents(temp_file);         if(!empty($cont))         {             echo '<div class="error"> Error Message:' . $cont . '</div>';             @unlink(temp_file);         }     } } 
like image 190
T.Todua Avatar answered Oct 02 '22 22:10

T.Todua


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.

like image 45
Eugene Kubovsky Avatar answered Oct 02 '22 21:10

Eugene Kubovsky