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

sometime it is because you use <?php ;?> unnecessary or use it like shown below

;?>

<?php

this extra line between closing and starting tag may also cause this error, simple remove that line/space

查看更多
看风景的人
3楼-- · 2018-12-31 14:14

My problem was that in the main php file, i had appended at the end of the file a javascript function. It seems that wordpress hooks such of a function in the head element. I externalised that function into a java script file.

Before:

<?php
/**
* Plugin Name: yyy
* Description: yyy
* Author: yyy
* Author URI: yyy
* Version: yyy
*/

/* many functions here */


function insert_in_header() {
    echo '<script type="text/javascript">',
    "perform_redirection(", '"', get_option('root'), '"', ", ", '"', get_option('redirect_to'), '");',
    '</script>';
}
add_action('wp_head', 'insert_in_header');
?>
<--! PROBLEMS HERE-->
<script type = "text/javascript">
    function perform_redirections(root, redirectionLink) {
      // code here
    }
</script>

After:

<?php
/**
* Plugin Name: yyy
* Description: yyy
* Author: yyy
* Author URI: yyy
* Version: yyy
*/

/* many functions here */

function insert_in_header() {
    // in headscripts.js i put the perform_redirection function 
    echo '<script type="text/javascript" src="', plugins_url('js/headscripts.js', __FILE__ ), '">  </script>';
    echo '<script type="text/javascript">',
    "perform_redirection(", '"', get_option('root'), '"', ", ", '"', get_option('redirect_to'), '");',
    '</script>';
}
add_action('wp_head', 'insert_in_header');
?>
查看更多
与风俱净
4楼-- · 2018-12-31 14:15

The error message The plugin generated *X* characters of unexpected output during activation is not very helpful or at least not quite enough.

To help locate the issue, add these to wp-config.php:

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

Then check wp-content/debug.log for more detailed error messages as to the origin of the error.

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

check here to see more info you can use:

<?php
     include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
            If (is_plugin_active('wshops/init.php'))
            { 
               //Run your plugin includes files or functions
            }

in your init php file.

查看更多
何处买醉
6楼-- · 2018-12-31 14:24

I had the same error - 3 characters of unexpected output and was lead here. For people in my scenario another cause of this message can be the file type being encoded as UTF with BOM.

BOM encoding was causing the error, and while the plug-in activated it would render incorrectly in internet explorer because of this.

The solution is to use Notepad++ and choose 'Convert to UTF without BOM', or if you are using visual studio, there is an explanation of how to change encoding UTF-8 without BOM

查看更多
听够珍惜
7楼-- · 2018-12-31 14:24

This problem can be solved by removing extra whitespaces. I solved this problem for my code. You can remove extra whitespaces easily in Adove Dreamweaver.

First, goto edit->Find and Replace. Or press Ctrl+F. Check "Use Regular Expression" button from "option" section.

Fill "find" field with the below code

[\r\n]{2,}

Fill "Replace" field with the below code

\n

Now click on "Replace All" button. Hope It will work.

查看更多
登录 后发表回答