插件在激活期间生成X个字符,这些字符意外输出(WordPress) [英] The plugin generated X characters of unexpected output during activation (WordPress)

查看:65
本文介绍了插件在激活期间生成X个字符,这些字符意外输出(WordPress)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次激活插件时,我都会收到此消息:

I'm getting this message each time I activate my plugin:

该插件在激活期间生成了80个字符的意外输出.如果您发现标题已发送"消息,联合供稿问题或其他问题,请尝试停用或删除此插件.

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.

我唯一可以抑制此消息的方法是将激活函数代码包装在 if 语句中(请参阅下面的代码段).

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

接下来,我将函数包装在 if 语句中的插件中;如上所述,它可以消除先前的错误:

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?

推荐答案

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

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.

通常,插件激活例程保留用于插件的基本设置,对set_option()之类的东西的调用.

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

这篇关于插件在激活期间生成X个字符,这些字符意外输出(WordPress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆