I’m tying to explain In-Depth about the WordPress plugin init file.
You are going to see:
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: ______________ */
- 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.
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
Step 2: Create a PHP file
sample-plugin.php into our new directory
Step 3: Copy and paste below code into
<?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.
Let’s activate our plugin by clicking on the link
After clicking on activate link you’ll get below error:
Error: Current WordPress and PHP versions do not meet minimum requirements for Sample Plugin.
Because Now (December 2019) WordPress latest version is
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
Requires at least: 5.0
and now try to Activate the plugin.
Error: Current PHP version does not meet minimum requirements for Sample Plugin.
Because of My current localhost PHP version
And, In our
sample-plugin.php file plugin header Requires PHP value is
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
* Requires PHP: 7.3.4
Now, If we click on the Activate.
Yup! Our Sample Plugin is now activated successfully.
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/
How WordPress internally detect the plugins? How WordPress internally detect the plugins?
By default WordPress load the plugins from the directory
We can change the plugins directory by defining the constant
WP_PLUGIN_DIR into the
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() read the file and detect the plugin metadata such as:
- Plugin Name
- Plugin URI
Let’s try some examples.
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
Step 2: Copy and paste below code into
<?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/
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
Step 2: Create a file
sample-plugin-2.php inside the
Step 3: Now copy below code into
<?php /** * Plugin Name: Plugin 2 */
Now, If we have more files then we can manage them in different directories like
Eg. We have added above two directories then our plugin format looks like below in the directory
assets/ includes/ sample-plugin.php
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
Step 2: Create a file
sample-plugin-3-one.php inside the
Step 3: Copy below code and paste into
<?php /** * Plugin Name: Plugin 3 One */
Step 4: Create a file
sample-plugin-3-two.php inside the
Step 5: Now, Copy below code into
<?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
In-depth of WordPress plugin init file.Tweet