1

I've stumbled on an activation error of the plugin, I'm developing and I can't seem to remove it. The error says "Plugin generate 2890 characters of unexpected output when activated". The purpose of my plugin is to view a banner at the website with information of an event happening. Both the banner and functionality works fine after the error message is shown.

My plugin is split into multiple files. I have the functionality in their seperate files and I have the view/HTML in another seperate file. I added an error log to the php script and it returns the HTML for the banner.

I have tried to turn the html into a Heredocs , but with no luck removing the error.

Do any of you have some advice on how to resolve the error of activation?

Thank you for your time.

Best regards Kristine

  • Are you also missing the opening <?php tag in your PHP file? – Jacob Peattie Oct 11 at 12:34
  • No they are all there :) – Kristine Kristensen Oct 11 at 12:43
  • Check that all statements in the global scope AND inside construct methods are only one of: define, add_action, add_filter, is* or class/function definitions. Anything else MAY output something upon load. – Knut Sparhell Oct 11 at 17:27
1

It is unfortunate core only gives you the string length when it has the entire output available to display. However you can find what those unexpected characters are by dumping them (from the output buffer) to a file on activation. Try adding this (outside the plugin you are activating of course, eg. in a PHP file in /wp-content/mu-plugins/)

<?php 
add_action( 'activated_plugin', 'debug_plugin_output', 10, 2 );
function debug_plugin_output( $plugin, $network_wide ) {
    $output = ob_get_contents();
    $file = WP_CONTENT_DIR . '/plugin-output.txt';
    if ( file_exists( $file ) ) {unlink( $file );}
    error_log( $output, 3,  $file);
}

And then after activation you can check the contents of /wp-content/plugin-output.txt... :-)

Alternatively, you could dump the output buffer directly and force exit (this will short-circuit multiple activations and prevent WordPress redirecting back to the plugin screen however):

<?php 
add_action( 'activated_plugin', 'debug_plugin_output', 10, 2 );
function debug_plugin_output( $plugin, $network_wide ) {
    $output = ob_get_contents();
    if ( !empty($output) ) {
        echo "Activation of plugin '" . $plugin . "'";
        echo " generated " . strlen( $output ) . " characters of unexpected output:<br><br>";
        echo "<textarea rows='100' cols='40'>" . $output . "</textarea>";
        exit;
    }        
}
0

Thank you so much for your help, I solved it by realising I had an include() statement outside of the functions in the plugin file. Then I moved the include() statement into their own function, which resulted in the activation error was solved :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.