douzhenggui8171 2016-12-14 19:39
浏览 126
已采纳

非常简单的Wordpress插件在尝试激活它时触发致命错误(PHP v 7)

I am diagnosing an issue with WP_cron(), so I wrote a very short app to list cron() tasks that are pending.

<? php

/*
    Plugin Name: My WP-Cron Test
    (there are more comments here but I left them out for brevity)
*/

function wpcron_print_tasks() {
    echo '<pre>';
    print_r( _get_cron_array() );
    echo '</pre>';
}

?>

Attempting to activate this tiny plugin leads to the following:

Plugin could not be activated because it triggered a fatal error. Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /latamconnex/wp-content/plugins/my-wp-cron-test/my-wp-cron-test.php on line 12

Line 12 triggers this according to the compiler, i.e. wpcron_print_tasks().

Somehow, even if this plugin only had a line with an echo command, it triggers the error.

Is there something basic I am missing about building a simple plugin?

Basically, I'd like to know what I need to know in order for even the simplest of plugins to activate.

System details: WordPress 4.7, PHP v. 7.0.13, deployed online with Pressable.com

  • 写回答

1条回答 默认 最新

  • donglu1881 2016-12-15 00:11
    关注

    Q1: Why is the error happening?

    Let's look at what the error says. The message you get tells you what the problem is and where it's throwing the error. Let's break it down.

    Plugin could not be activated because it triggered a fatal error. Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /latamconnex/wp-content/plugins/my-wp-cron-test/my-wp-cron-test.php on line 12

    1. It's a "syntax error"
    2. It's located in the file /latamconnex/wp-content/plugins/my-wp-cron-test/my-wp-cron-test.php
    3. The error is thrown on line 12 of that file.

    It's telling you there is a syntax error on or before line 12 of that file.

    Looking at your code, line 1 has an error in it. Do you see it? You have <? php on line 1. PHP is not going to recognize that line as the opening PHP tag. Why? There's a space in between the ? and php.

    Change that line to <?php. Notice, there's no space.

    Make sure there are no other syntax errors between line 1 and 12.

    Tip: Remove the Closing PHP Tag

    You don't need the closing PHP tag ?> at the end of the file. I recommend removing the closing PHP tag.

    Why? Any extra spaces or lines after that tag will cause the dreaded white screen of death. You can see the conversation about it here on Stack Overflow.

    The best practice here is to omit the optional closing tag from all PHP files.

    Q2:

    Basically, I'd like to know what I need to know in order for even the simplest of plugins to activate.

    First, WordPress has to recognize your code as a "plugin." Then once it finds it, then it's available for activation. There does not have to be a single line of code in the file for it to be activated.

    How does WordPress recognize the plugin?

    WordPress searches each folder in the wp-content/plugins folder. It's looking in the root of each folder for a bootstrap file.

    What makes a file the "bootstrap?"

    It's the file header (DocBlock) that identifies the plugin and makes it available to WordPress. That's your bootstrap file.

    WordPress searches in the plugin folder's root for the file that has the properly structured file header.

    The structure is defined in this Header Requirements document.

    What should be in a plugin file?

    All that's needed for the file to be activated is:

    1. The opening PHP tag, i.e. <?php
    2. The file's DocBlock

    That's it. Now it's added to the list of plugins in Plugins > Installed Plugins (in the WordPress back-end admin).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题