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:29

In my case it was due to Undefined index, Just enable the debug log to check whats causing it, then you can resolve it easily.

For those who don't know how to enable the Debug log, Add these lines in your wp-config.php:

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_DISPLAY', true );
    define( 'WP_DEBUG_LOG', true );

You can see the errors properly in the debug file created in wp-content

查看更多
孤独总比滥情好
3楼-- · 2018-12-31 14:30

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.

查看更多
看淡一切
4楼-- · 2018-12-31 14:30

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);
        }
    }
}
查看更多
栀子花@的思念
5楼-- · 2018-12-31 14:31

I battled this problem for a long time. Typically this is caused by spaces or new lines before the opening <?php tag or after the closing ?> tag. Once I removed these, the error went away.

Also, never assume anything about GET, POST, COOKIE and REQUEST variables. Always check first using isset() or empty().

查看更多
琉璃瓶的回忆
6楼-- · 2018-12-31 14:35

A common way to assign the register_activation_hook is using a static method of a class. This ensures that your plugin activation function name won't collide with other plugins.

class Foo_Plugin 
{
    public static function plugin_activation() {
        // activation logic
    }
}

This function needs to be public and not private. A mistake is easily made though, so this could be a reason for your problems when getting this kind of error.

You would then register the activation with this code in the main plugin file.

register_activation_hook( __FILE__, array( 'Foo_Plugin', 'plugin_activation' ) );
查看更多
梦醉为红颜
7楼-- · 2018-12-31 14:36

I was having the same issue, i tried to remove the code and did everything but still the same problem

the real solution for me was the following.

  1. at the end of the file which contains the header of the plugin i removed closing php ?> and the problem will be solved
  2. at the end of the plugin file which contains the header of the plugin remove the extra line breaks after the closing php ?>

my plugin file ended at line 69 and there were 7 more spaced after the last php code so in the editor the file was up to 66, my error was "The plugin generated 7 characters of unexpected...." when i removed extra lines up to 69 the was error gone,

Thanks

查看更多
登录 后发表回答