最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress plugin - Error "Plugin generate 2890 characters of unexpected output when activated"

programmeradmin0浏览0评论

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

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

Share Improve this question asked Oct 11, 2019 at 12:22 Kristine KristensenKristine Kristensen 134 bronze badges 3
  • Are you also missing the opening <?php tag in your PHP file? – Jacob Peattie Commented Oct 11, 2019 at 12:34
  • No they are all there :) – Kristine Kristensen Commented Oct 11, 2019 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 Commented Oct 11, 2019 at 17:27
Add a comment  | 

2 Answers 2

Reset to default 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;
    }        
}

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

发布评论

评论列表(0)

  1. 暂无评论