Creating a simple WordPress plugin

As an example, let us create a simple plugin and install it. Our plugin is going to be a single php file. Our plugin is going to be called Sytech Academy Digital Classroom.

Plugin code

Create an empty file named aca-sytech-plugin.php. Copy the following code into the file and save it.

<?php
/*
Plugin Name: Sytech Academy Digital Classroom
Version: 1.0
Description: Creates a digital classroom where students can learn the anything set by the tutor.
Author: Sydney Chako
Author URI: https://sytech.co.zw
Plugin URI: https://sytech.co.zw/digital-classroom/
*/


/* Version check */
global $wp_version;
 
$exit_msg='Sytech Academy Digital Classroom requires WordPress 3.5 or newer. <a href="http://codex.wordpress.org/Upgrading_WordPress">Please update!</a>';

if (version_compare($wp_version,"3.5","<")){
    exit ($exit_msg);
    }
?>

Installing and Activating the plugin

  • After saving your plugin file, upload it to the wp-content/plugins folder on your server using your FTP.
  • Go to your WordPress installed plugins on admin panel. If everything when well, you should now see your plugin listed among other plugins:

creating a simple wordpress plugin

  • Congratulations, you have just created a WordPress plugin.
  • You can even go ahead and activate the plugin now, although it does not do anything useful yet.

Activate wordpress plugin

Now, let us do a summary of what we have accomplished so far in this example:

  • We created a working plugin (yes, it worked because it didn’t crash your site) by using a plugin information header and the version check code.
  • The purpose of the plugin header is for the plugin to be identified and displayed properly in the installed plugins admin panel. It is written as a PHP comment and contains several fields with important information that is needed for the plugin to be registered, displayed properly in the admin panel.
  • The version check code warns users who have older WordPress versions to upgrade their WordPress installation and prevent plugin compatibility problems. WordPress provides the global variable $wp_version that provides the current WordPress installation version. We then used the PHP function version_compare() to comparethe WordPress installation version to our required version for the plugin.
  • To stop the execution of the plugin upon activation, we use the exit() function with the error message we want to show.
  • If your WordPress version is newer than version 3.5, you wont see the error message.

Plugin Header Requirements

As we have seen in the example above, a WordPress plugin is simply a Php file or a folder containing Php files. It should a main PHP file that includes a header as a Php comment. The header is what tells WordPress that a file is a plugin and also provides more information about the plugin. A Php file with nothing else but valid a header comment is detected as a valid plugin as long as its in the plugins folder.


For example:

<?php
/*
Plugin Name: Sytech Academy Digital Classroom
Version: 1.0
Description: Creates a digital classroom where students can learn the anything set by the tutor.
Author: Sydney Chako
Author URI: https://sytech.co.zw
Plugin URI: https://sytech.co.zw/digital-classroom/
*/
?>

Information from the plugin header will be shown when the plugin is installed, as follows:

WordPress plugin description

Minimum plugin header fields required

The minimum information required in the plugin is the Plugin Name only.

Which means the following file is a valid plugin file:

<?php

/**
 * Plugin Name: YOUR PLUGIN NAME
 */

?>

You can even install it and activate it. However in terms of functionality, it doesn’t do anything besides being detected as a WordPress plugin.

See:

Plugin description

Available plugin header fields:

  • Plugin Name: (required) The name of your plugin, which is to be displayed in the Plugins list in the WordPress Admin.
  • Plugin URI: The unique home page URL of the plugin, preferably on your own website. You cannot use a WordPress.org URL here.
  • Description: A short honest description of the plugin, which will be displayed in the Plugins section in the WordPress Admin. It should be less than 140 characters.
  • Version: The current version number of the plugin, such as 1.0 or 1.0.3.
  • Requires at least: The lowest WordPress version that the plugin will work on.
  • Requires PHP: The minimum required PHP version for the plugin to work correctly.
  • Author: The name of the plugin author (Your name). Multiple authors may be listed using commas.
  • Author URI: The author’s website or profile on another website, such as WordPress.org.
  • License: The short name (slug) of the plugin’s license (e.g. GPLv2).
  • License URI: A link to the full text of the license (e.g. https://www.gnu.org/licenses/gpl-2.0.html).
  • Text Domain: The gettext text domain of the plugin.
  • Domain Path: The domain path lets WordPress know where to find the translations.
  • Network: Whether the plugin can only be activated network-wide. Can only be set to true, and should be left out when not needed.

For example:

<?php
/**
 * Plugin Name:       Plugin Name
 * Plugin URI:        https://example.com/plugins/plugin-name/
 * Description:       This plugin provides blah blah blah
 * Version:           3.23.3
 * Requires at least: 5.7
 * Requires PHP:      7.4
 * Author:            Your Name
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       my-plugin-name
 * Domain Path:       /languages
 */

?>

Triggering errors

Now to test the error message:

  • copy the code of the footer message plugin we created above
  • change the expected WordPress version to 10.5 as follows:

$exit_msg='Sytech Academy Digital Classroom requires WordPress 10.5 or newer. <a href="http://codex.wordpress.org/Upgrading_WordPress">Please update!</a>';

if (version_compare($wp_version,"10.5","<")){
    exit ($exit_msg);
    }
  • now reactivate the plugin. The plugin won’t reactivate and you will receive the following error:

Wordpress plugin error screen

Of course, at the point of writing WordPress 10.5 is not yet out! The current latest version is 5.7.