0

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

Eamon Bohan
  • 522
  • 2
  • 8
  • 22

1 Answers1

1

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).

Community
  • 1
  • 1
hellofromTonya
  • 1,301
  • 8
  • 8