php 该插件在激活期间生成了 X 个意外输出字符 (WordPress)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4074477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:52:09  来源:igfitidea点击:

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

phpwordpress

提问by Scott B

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.

该插件在激活期间生成了 80 个字符的意外输出。如果您注意到“标头已发送”消息、联合订阅源问题或其他问题,请尝试停用或删除此插件。

The only way I was able to suppress the message was to wrap my activation function code within an ifstatement (please refer to snippets below).

我能够抑制该消息的唯一方法是将我的激活函数代码包装在if语句中(请参阅下面的片段)。

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 ifstatement; it suppresses the previous error as discussed above:

接下来,我将函数包装在我的插件中的if语句中;它抑制了前面讨论的错误:

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?

有没有更好的方法来处理这个问题?

采纳答案by jay.lee

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.

我认为这里可能有两个问题导致了这个问题。首先,我不认为 wordpress 在调用插件激活挂钩时需要任何输出,因此它可能会对此有所抱怨。其次,插件激活挂钩在 wordpress 程序流中很早就被调用,因此,它可能在发送标头之前被调用。如果在调用之前生成了任何输出,header()那么 PHP 通常会抱怨。

Usually the plugin activation routine is reserved for basic setup of the plugin, calls to things like set_option()and the like.

通常插件激活例程保留用于插件的基本设置,调用诸如此类set_option()的东西。

回答by T.Todua

2 probably reasons:

2个可能的原因:

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

1)您正在echo错误的地方进行输出(例如或等)。

  • Do you want to output a message in admindashboard? - use admin_noticeshook and output there...
  • Do you want to output a message in front-end? - find appropriate places with hooks (like the_contentor wp_footeror whatever).

  • Don't output anything either in register_activation_hookor outside of WordPress standard hooks, no-one should do that.**

  • 您想在admin仪表板中输出消息吗?- 使用admin_notices钩子并在那里输出......
  • 你想在前端输出一条消息吗?- 用钩子找到合适的地方(比如the_contentwp_footer或其他)。

  • 不要在register_activation_hookWordPress 标准钩子之内或之外输出任何内容,任何人都不应该这样做。**

2) if you aren't doing any output intentionally, then maybe some phperror happens? If so, put this code temporarily in functions.phpand then activate the plugin - you will see the error.

2)如果你没有故意做任何输出,那么可能php会发生一些错误?如果是这样,请暂时放入此代码functions.php,然后激活插件 - 您将看到错误。

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);
        }
    }
}

回答by Eugene Kubovsky

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.

有同样的错误,但只有 6 个字符)所以......在我的情况下,我在 PHP 结束标记之后有空行?> - 这也会导致这个错误。

回答by Ewart Maclucas

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.

我遇到了同样的错误 - 3 个字符的意外输出并被引导到这里。对于我的场景中的人,此消息的另一个原因可能是文件类型被编码为带有 BOM 的 UTF。

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

BOM 编码导致错误,当插件激活时,它会因此在 Internet Explorer 中呈现错误。

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

解决方案是使用 Notepad++ 并选择“Convert to UTF without BOM”,或者如果您使用的是visual studio,有关于如何在没有 BOM 的情况下更改编码UTF-8的说明

回答by kaleazy

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

我与这个问题斗争了很长时间。通常这是由开始<?php标记之前或结束?>标记之后的空格或换行引起的。一旦我删除了这些,错误就消失了。

Also, never assume anything about GET, POST, COOKIEand REQUESTvariables. Always check first using isset()or empty().

另外,不要假设任何事情GETPOSTCOOKIEREQUEST变量。始终首先使用isset()或 进行检查empty()

回答by Marqas

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:

对于那些不知道如何启用调试日志的人,请在 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

您可以在 wp-content 中创建的调试文件中正确查看错误

回答by Figidon

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.

分配 register_activation_hook 的常用方法是使用类的静态方法。这确保您的插件激活函数名称不会与其他插件冲突。

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' ) );

回答by Aamer Shahzad

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 ?>
  1. 在包含插件标题的文件末尾,我删除了关闭 php ?> 问题将得到解决
  2. 在包含插件标题的插件文件末尾删除关闭 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,

我的插件文件在第 69 行结束,在最后一个 php 代码之后还有 7 个空格,所以在编辑器中文件最多为 66,当我删除额外的行时,我的错误是“插件生成了 7 个意外的字符......”高达 69 错误消失了,

Thanks

谢谢

回答by Fabien Snauwaert

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

错误消息The plugin generated *X* characters of unexpected output during activation不是很有帮助,或者至少还不够。

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

为了帮助定位问题,请将这些添加到wp-config.php

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

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

然后检查wp-content/debug.log有关错误来源的更详细的错误消息。

回答by M.S Shohan

I had the same problem. I noticed that there were new lines at the beginning of the file. So I removed them and the errors disappeared. Try removing the new lines at the beginning of your files. That may help.

我有同样的问题。我注意到文件开头有新行。所以我删除了它们,错误消失了。尝试删除文件开头的新行。这可能会有所帮助。