Last updated on September 13, 2023

Create a Plugin

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Build a custom plugin from scratch.

Creating a simple plugin in WordPress is fairly easy. Here are the steps you need to follow:

  1. Create a new folder in the wp-content/plugins directory of your WordPress installation. You can name the folder anything you like. For example, my-plugin.
  2. Create a new PHP file inside the my-plugin folder and name it anything you like with the .php extension. For example, my-plugin.php.
  3. Add the following code to your my-plugin.php file:
/**
 * Plugin Name:  My Plugin
 * Description:  A brief description of your plugin.
 * Version:      1.0
 * Author:       Your Name
 * Author URI:   https://yourwebsite.example.com/
 */

// Plugin code goes here

This code creates the WordPress plugin and sets some important metadata such as the plugin name, version, author, etc. Other possible fields to add to the header comments are things like:

  • Plugin URI: A URL to the website of the plugin
  • Requires at least: The minimum required WordPress version
  • Requires PHP: The minimum required PHP version
  • License: A slug of the license type
  • License URI: URL to the full licence text
  • Text Domain: The text domain of the plugin
  • And more!

For a detailed description of all available fields, please see the section Header Requirements of the Plugin Handbook on WordPress.org. Here you will find a list of all fields, as well as a description.

A more detailed header comment for a plugin might look like this:

/**
 * Plugin Name:       My Cool Plugin
 * Plugin URI:        https://example.com/my-cool-plugin/
 * Description:       This is a very cool plugin.
 * Version:           1.0.0
 * Requires at least: 5.1
 * Requires PHP:      7.4
 * Author:            Jane Doe
 * Author URI:        https://janedoe.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       my-cool-plugin
 * Domain Path:       /languages
 */

// Plugin code goes here

When you upload this file to the wp-content/plugins folder, it will show up under Plugins in WP Admin, like so:

Registering actions to run on activation/deactivation of the plugin

Sometimes you need a function to run whenever your plugin is activated or deactivated. For this purpose, you can use the functions register_activation_hook() and register_deactivation_hook(). Here’s how you do it:

/**
 * Plugin Name: My Cool Plugin
 */

// Define which functions should run on activation and/or deactivation
register_activation_hook( __FILE__, 'function_to_run_on_activation' );
register_deactivation_hook( __FILE__, 'function_to_run_on_deactivation' );

function function_to_run_on_activation() {
   // Code to run on activation goes here
}

function function_to_run_on_deactivation() {
   // Code to run on deactivation goes here
}

Best practices for WordPress plugin development

When you are working on a custom plugin, a few things are important to keep in mind. One of the most important things, is using prefix for everything.

“All variables, functions and classes should be prefixed with a unique identifier. Prefixes prevent other plugins from overwriting your variables and accidentally calling your functions and classes. It will also prevent you from doing the same.”

WordPress Plugin Handbook

Another way to prevent running into naming collisions is creating a plugin using an OOP approach. This means you create a PHP class for all the code of your plugin.

/**
 * Plugin Name: My Plugin
 */

if ( ! class_exists( 'My_Plugin' ) ) {
    class My_Plugin {

        public static function init() {
            // Code to intialize the plugin goes here
        }
    }
}

// Initialize the plugin
$my_plugin = new My_Plugin();
$my_plugin->init();

File structure and plugin directories

A simple file structure in plugins could look something like this:

  • wp-content/plugins/my-plugin
    • assets: Contains all media assets
    • admin: Contains the code for the “back end” of the plugin
    • css: Contains all front end stylesheets
    • js: Contains all front end JavaScript
    • includes: Contains all the classes and functions
    • my-plugin.php: Loads the plugin file

Some plugins choose to place the css en js folders in the assets folder.

Get your plugin in the WordPress plugin repository

Should you wish to do so, you can submit your plugin to the WordPress plugin repository.

Last updated on September 13, 2023. Originally posted on April 27, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *