In-depth of WordPress plugin init file

Advertisement

Init File Structure Init File Structure

Below is the structure of the example plugin’s init file.

<?php
/**
 * Plugin Name: ______________
 * Plugin URI: ______________
 * Description: ______________
 * Author: ______________
 * Author URI: ______________
 * Version: ______________
 * Text Domain: ______________
 * Domain Path: ______________
 * Network: ______________
 * Requires at least: ______________
 * Requires PHP: ______________
 */

Here,

  • Plugin Name – It is an actual plugin name that should be unique.
  • Plugin URI – Link of the plugins website address.
  • Description – It is a long description of the plugin.
  • Author – Author name of the plugin.
  • Author URI – Author’s website address (if set).
  • Version – Latest/Current plugin version.
  • Text Domain – Plugin text-domain.
  • Domain Path – Plugins relative directory path to .mo files.
  • Network – Whether the plugin can only be activated network-wide.
  • Requires at least – Minimum required a version of WordPress.
  • Requires PHP – Minimum required a version of PHP.

The plugin headers Requires at least and Requires PHP added from version 5.3.0.

Top ↑

Sample Init file Sample Init file

To understand the init file structure, Let’s create a plugin that elaborates more about the plugin init file headers.

Follow the below steps to create our new plugin called “Sample Plugin“.

Step 1: Create a folder sample-plugin into /wp-content/plugins/ directory.

Step 2: Create a PHP file sample-plugin.php into our new directory /wp-content/plugins/sample-plugin/.

Step 3: Copy and paste below code into sample-plugin.php file.

<?php
/**
 * Plugin Name: Sample Plugin
 * Plugin URI: http://maheshwaghmare.com/
 * Description: My Sample Plugin Description Goes Here..
 * Author: Author Name
 * Author URI: https://profiles.wordpress.org/mahesh901122/
 * Version: 1.0.0
 * Text Domain: sample-plugin
 * Domain Path: /languages/
 * Requires at least: 6.0
 * Requires PHP: 9.0
 */

Now, Our Sample Plugin listed into the plugins list and looks like the below screenshot.

“Sample Plugin” listed into the plugins page.

Let’s activate our plugin by clicking on the link Activate.

After clicking on activate link you’ll get below error:

Error: Current WordPress and PHP versions do not meet minimum requirements for Sample Plugin.

Why?

Because Now (December 2019) WordPress latest version is 5.3.

And, In our sample-plugin.php file plugin header Requires at least value is 6.0 which describe that our plugin requires WordPress version at least 6.0.

For that, we get the above error. I have set value 6.0 just for demonstration purposes.

Now to fix the error by changing the value of Requires at least from our plugin init file from:

 * Requires at least: 6.0

with

Requires at least: 5.0

and now try to Activate the plugin.

Error: Current PHP version does not meet minimum requirements for Sample Plugin.

Why?

Because of My current localhost PHP version 7.3.0.

And, In our sample-plugin.php file plugin header Requires PHP value is 9.0.

I have PHP Version 7.3.4. So, If I change the value of Requires PHP with LOWER or EQUAL than the version 7.3.4 then it works.

So, To fix the above error change the Requires PHP from:

 * Requires PHP: 9.0

with

 * Requires PHP: 7.3.4

Now, If we click on the Activate.

“Sample Plugin” activate.

Yup! Our Sample Plugin is now activated successfully.

IMPORTANT

I have set the WordPress and PHP version accoding to my current configuration to demostrate the errors. But, While developing the plugin you need to focus on your customer’s configurations.
Basically we need to support recent 2 major versions of WordPress iteself wile developing plugin or theme. But, You can change it accoding to your customer base. Or See how much user use which version of WordPress from https://wordpress.org/about/stats/ and add support for maximum users.

Now (December 2019) WordPress iteself set PHP minimum requirement 7.3 or greater.
Read more https://wordpress.org/about/requirements/

Top ↑

How WordPress internally detect the plugins? How WordPress internally detect the plugins?

By default WordPress load the plugins from the directory /wp-content/plugins/.

We can change the plugins directory by defining the constant WP_PLUGIN_DIR into the wp-confing.php file.

E.g.

define( 'WP_PLUGIN_DIR', '/path/');

WordPress read each .php file from /wp-content/plugins/ directory and gather the plugin metadata with function get_plugin_data().

The function get_plugin_data() read the file and detect the plugin metadata such as:

  • Plugin Name
  • Plugin URI
  • Version
  • Description
  • Author
  • etc.

Let’s try some examples.

Top ↑

Understanding plugins with examples Understanding plugins with examples

Example 1 Example 1

Plugin with Single PHP file.

Suppose, we have a single PHP file that contains a small piece of code.

Step 1: Create a file sample-plugin-1.php inside the /wp-content/plugins/ directory.

Step 2: Copy and paste below code into sample-plugin-1.php file.

<?php
/**
 * Plugin Name: Plugin 1
 */

Now, our plugin is visible in the plugins list.

Note: I have just added the Plugin Name for demonstration. But, Other plugin headers are also IMPORTANT when we release and host our plugin on https://wordpress.org/plugins/

Top ↑

Example 2 Example 2

Plugin with its own directory.

Suppose, we have a large plugin that contains a lot of files. So, To maintain it let’s create the plugins own directory.

Step 1: Create a directory sample-plugin-2 inside the /wp-content/plugins/ directory.

Step 2: Create a file sample-plugin-2.php inside the /wp-content/plugins/sample-plugin-2/ directory.

Step 3: Now copy below code into sample-plugin-2.php file.

<?php
/**
 * Plugin Name: Plugin 2
 */

Now, If we have more files then we can manage them in different directories like assets, includes etc.

Eg. We have added above two directories then our plugin format looks like below in the directory /wp-content/sample-plugin/:

assets/
includes/
sample-plugin.php

Top ↑

Example 3 Example 3

Two or more plugin in a single directory.

Suppose, we have two plugins but we want to keep them in ONE single directory.

In WordPress, we can do it.

Step 1: Create a directory sample-plugin-3 inside the /wp-content/plugins/ directory.

Step 2: Create a file sample-plugin-3-one.php inside the /wp-content/plugins/sample-plugin-3/ directory.

Step 3: Copy below code and paste into sample-plugin-3-one.php

<?php
/**
 * Plugin Name: Plugin 3 One
 */

Step 4: Create a file sample-plugin-3-two.php inside the /wp-content/plugins/sample-plugin-3/ directory.

Step 5: Now, Copy below code into sample-plugin-3-two.php file.

<?php
/**
 * Plugin Name: Plugin 3 Two
 */

Here, The most interesting thing we have to create two plugins in a single directory.

And, we can activate or deactivate our plugin Plugin 3 One and Plugin 3 Two independently.

Our plugin structure looks like below into /wp-content/plugins/sample-plugin-3/ directory.

sample-plugin-3-one.php
sample-plugin-3-two.php

In-depth of WordPress plugin init file.

One thought on “In-depth of WordPress plugin init file

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: