Building a Simple WordPress Plugin

Posted on 19th June 2023

WordPress plugins are small programs that extend the functionality of the WordPress core software. They offer custom features and enhancements to your website that are not available in the core software. In this article, we will show you how to build a simple WordPress plugin.

What You Will Need

Before you begin this tutorial, you should have a local development environment set up on your computer. You will need access to a text editor and a web browser. We will be using WordPress version 4.9.8 for this tutorial.

You will also need to be familiar with the following:

  • The WordPress Codex
  • The WordPress Code Standards
  • How to Create a Plugin for WordPress

Creating a Plugin

First, you need to create a new directory for your plugin. For this example, we will call it “my-plugin”.

Next, you need to create a new file in your “my-plugin” directory. The file should be named “my-plugin.php”.

Now, you need to open “my-plugin.php” in your text editor and add the following code:


<?php
/*
Plugin Name: My Plugin
Plugin URI: https://example.com/my-plugin
Description: This is a simple WordPress plugin.
Version: 1.0
Author: John Doe
Author URI: https://example.com
License: GPLv2 or later
*/
?>

This is the minimum code you need for a WordPress plugin. The first line is the opening PHP tag. The next line is the plugin name. The plugin name can be anything you want. In our example, we have used “My Plugin”.

The next line is the plugin URI. This is the URL of the plugin’s website. In our example, we have used https://example.com/my-plugin.

The next line is the description. This is a short description of what your plugin does. In our example, we have used “This is a simple WordPress plugin.”

The next line is the version number. This is the version number of your plugin. In our example, we have used “1.0”.

The next line is the author. This is the author’s name. In our example, we have used “John Doe”.

The next line is the author URI. This is the author’s website. In our example, we have used https://example.com.

The next line is the license. This is the license your plugin is released under. In our example, we have used “GPLv2 or later”.

The last line is the closing PHP tag. This is the end of the plugin code.

Activating Your Plugin

Once you have saved your plugin, you need to activate it. You can do this by going to the “Plugins” page in the WordPress admin area and clicking on the “Activate” link for your plugin.

Once your plugin is activated, you will see a “Success” message. You can now go to the “Settings” page and configure your plugin.

Conclusion

In this article, we have shown you how to build a simple WordPress plugin. You should now have a good understanding of how plugins work and how you can create your own plugins.

When you’re ready to start coding, create a new folder in your wp-content/plugins directory. Name this folder after your plugin, replacing any spaces with hyphens. For example, if your plugin is named “My Awesome Plugin,” you would name the folder my-awesome-plugin.

Next, create your plugin’s main PHP file. The name of this file should be the same as your plugin’s folder, with the .php extension added. So, continuing with the example above, the main PHP file for the plugin would be named my-awesome-plugin.php.

At the top of this file, you’ll need to add some basic information about your plugin. This information is enclosed in PHP comments, which look like this:

This information is used by WordPress to display information about your plugin on the Plugins screen, as well as the Plugin Details popup. It’s important to fill out each field accurately.

The Plugin Name field is the name of your plugin as it will appear on the Plugins screen.

The Plugin URI field is a link to your plugin’s home page. This is usually your own website.

The Description field is a short description of your plugin. This description is displayed on the Plugins screen.

The Version field is the version number of your plugin. As you release new versions of your plugin, you should update this number.

The Author field is your name or the name of your company.

The Author URI field is a link to your website.

The License field is the license under which your plugin is released. The most common license for WordPress plugins is the GNU Public License (GPL).

The Text Domain field is used to internationalize your plugin. This is beyond the scope of this article, but if you plan to release your plugin in multiple languages, you should read the WordPress documentation on internationalization.

After the plugin information, you can start adding your plugin’s code. In our example, we’ll add a simple function that outputs “Hello, world!” when the plugin is activated.

This code adds a function named my_awesome_plugin_activate(). This function is triggered when the plugin is activated. In this function, we simply output the string “Hello, world!”

Finally, we need to tell WordPress to run our function when the plugin is activated. We do this with the register_activation_hook() function. This function accepts two parameters: the path to the main plugin file, and the name of the function to run. In our example, we use the PHP magic constant __FILE__ for the first parameter, which contains the path to the current file.

If you now activate your plugin, you should see “Hello, world!” printed on the page.

Next, let’s add a function that will be run when the plugin is deactivated. We’ll add this function to the my-awesome-plugin.php file, right after the my_awesome_plugin_activate() function:

This code is similar to the code we added for the activation function. We create a function named my_awesome_plugin_deactivate(), which outputs the string “Goodbye, world!” We then register this function with the register_deactivation_hook() function.

If you now deactivate your plugin, you should see “Goodbye, world!” printed on the page.

Now that we’ve added functions for when the plugin is activated and deactivated, let’s add a function that will be run when the plugin is loaded. We’ll add this function to the my-awesome-plugin.php file, right after the my_awesome_plugin_deactivate() function:

This code is similar to the code we added for the activation and deactivation functions. We create a function named my_awesome_plugin_load(), which outputs the string “This plugin is loaded!” We then register this function with the add_action() function.

The first parameter of the add_action() function is the name of the action hook to which we want to attach our function. In this case, we use the plugins_loaded hook, which is triggered when all plugins have been loaded.

The second parameter is the name of the function to run.

If you now load a page on your WordPress site, you should see “This plugin is loaded!” printed on the page.

We’ve now added functions for when the plugin is activated, deactivated, and loaded. In the next section, we’ll add a function that will be run when a user saves a post.

We’ll add this function to the my-awesome-plugin.php file, right after the my_awesome_plugin_load() function:

<?php
/*
Plugin Name: My Awesome Plugin
Plugin URI: https://example.com/my-awesome-plugin
Description: This plugin is awesome!
Version: 1.0
Author: John Doe
Author URI: https://example.com
License: GPLv2 or later
Text Domain: my-awesome-plugin
*/

// This is where your code will go

function my_awesome_plugin_activate() {
echo 'Hello, world!';
}
register_activation_hook( __FILE__, 'my_awesome_plugin_activate' );

function my_awesome_plugin_deactivate() {
echo 'Goodbye, world!';
}
register_deactivation_hook( __FILE__, 'my_awesome_plugin_deactivate' );

function my_awesome_plugin_load() {
echo 'This plugin is loaded!';
}
add_action( 'plugins_loaded', 'my_awesome_plugin_load' );

function my_awesome_plugin_save_post( $post_id ) {
echo 'The post with ID ' . $post_id . ' was saved!';
}
add_action( 'save_post', 'my_awesome_plugin_save_post' );

?