简体   繁体   English

Wordpress插件错误PHP

[英]Wordpress Plugin Error PHP

When trying to activate my plugin, wordpress comes up with this error: 尝试激活我的插件时,wordpress出现以下错误:

Plugin could not be activated because it triggered a fatal error. 无法激活插件,因为它触发了致命错误。 Parse error: syntax error, unexpected T_FUNCTION on line 79 解析错误:语法错误,第79行出现意外的T_FUNCTION

The line 79 is the first line of the snippet below, from looking into this I think it is because of a PHP version error, since I have no control over updating php, how would I make it compatible with earlier versions? 第79行是下面的代码片段的第一行,从调查来看,我认为这是因为PHP版本错误,由于我无法控制更新php,我如何使其与早期版本兼容?

add_action('admin_menu', function(){
        Plugin_Options::add_menu_page();
    });

Your plugin requires PHP version 5.3.x for function , earlier versions of PHP give you that syntax error message. 您的插件需要PHP 5.3.x版才能使用该function ,早期版本的PHP会为您提供该语法错误消息。

Wordpress does not offer a mechanism to make plugins tell which dependency they need so they need to be activated and care on their own (or just fail as in your case). WordPress并未提供一种机制来使插件知道所需的依赖关系,因此需要激活插件并自行维护(否则就失败了)。

You can just add it this way instead: 您可以这样添加它:

add_action('admin_menu', 'Plugin_Options::add_menu_page');

And done. 并做了。 That's a static class method call (As of PHP 5.2.3, Type 4 callable in the Callback Example Docs ), PHP 5.2.3 is within the minimum PHP version requirements of wordpress (that's since WordPress 3.1), so this looks like the preferred method. 那是一个静态的类方法调用(从PHP 5.2.3开始, 在Callback Example Docs中可以调用Type 4 ),PHP 5.2.3在wordpress的最低PHP版本要求之内 (从WordPress 3.1开始),因此这似乎是首选的方法。

Most likely, your PHP version is < 5.3. 您的PHP版本很可能低于5.3。 Anonymous functions were added to PHP in 5.3. 匿名功能已在5.3中添加到PHP。 In order to make use of this, you can pass the function as a callback string like: 为了利用此功能,您可以将函数作为回调字符串传递,例如:

function add_menu_callback() {
  Plugin_Options::add_menu_page();
}

add_action('admin_menu', 'add_menu_callback');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM