Skip to content

Tutorial: Creating WordPress Plugins – Step by Step

  • 6 min read
  • by

Introduction

WordPress is a very popular blogging platform. But that’s not all it’s good for. It can also function as an portfolio, business site, or e-commerce store. However the WordPress Core isn’t enough for WordPress to function like these. Instead, users need to extend WordPress’s functionality. How? The answer is through plugins? Plugins are essentially a collection of files that add to how WordPress functions.

There are many kinds of plugins, free or paid, simple or complex. But what happens when WordPress users find that the functionality they need isn’t available in any plugin? Well, they can hire a developer. However, an even better option is available, particularly if they have software development experience. That option is to create their own plugins. By creating their own plugins, users can add functionality of their own design, scope, and complexity. If needed, they can even modify and even enhance the functionality of other plugins.

In this tutorial, we’ll show you how to get started. We will be showing how to create a basic WordPress plugin.

Steps to Create a WordPress Plugin

1. Create an empty PHP file

What we need to do first is create an empty PHP file. To do that, we open a Text Editor and then save it with a “.php” extension. You can save the file in any directory.

create an empty PHP file and save it

2. Place header comments containing plugin information

We need to place a header comment on the top of the plugin. This comment states the the file is a plugin. The comment should contain information about the plugin like name, author, and description (Note: it should at least contain a Plugin Name). You can also add other fields if you need to.

Here is our header comment for our plugin.

<?php
/**
* Plugin Name: Our Plugin
* Description: This is the very first plugin we have ever created.
* Version: 1.0
* Author: A WordPress User
**/Code language: HTML, XML (xml)

Here is the code snippet above in an actual file.

code snipet

3. Install the plugin through WordPress

We can then install the plugin on WordPress. The easiest way to do that, if the site is deployed locally, is just to go the WordPress site’s “wp-content/plugins” folder and paste the plugin PHP file there.

install the plugin

If the WordPress site is deployed remotely, you need to have FTP access to the remote server. To be more specific, you need access to the remotely installed WordPress site’s “wp-content/plugins” folder. Once you have that, you just need to proceed by uploading the plugin PHP file through an FTP Client

upload the file

Once that is done, navigate to the “Installed Plugins” page on the WordPress admin dashboard. You will be able to see that the plugin listed there, ready to be activated when needed.

plugin in the plugins overview

4. Adding functionality to the plugin

To add functionality, we need to add custom functions in the PHP file. Without any code, the plugin cannot do anything. We need to decide what the plugin can do, and then add functions that can provide these functions.

Let’s do just that let’s create a plugin that displays text on the admin dashboard. (This is similar to the “Hello Dolly” function). This is our function:

function hello_wordpress () {
    echo "HELLO WORDPRESS";
}Code language: PHP (php)

However, this isn’t enough to get the plugin running. We need to use it in conjunction (this is called ‘registering’) with a WordPress hook. You may be wondering what hooks are. According to the WordPress Plugin Handbook:

Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots.

Simply put, this allows code to run in specific places when WordPress us running. There are two types of hooks – “Actions” and “Filters”. Actions allows functions to execute at specific points when the WordPress Core, plugins, or themes are executing. Filters on the other hand, modify the data which will be displayed or saved.

To use functions when an action occurs, we use:

add_action('action_name', 'custom_action_function');Code language: JavaScript (javascript)

This would execute ‘custom_function_name’ when ‘action_name’ occurs.

For filters we would use:

add_filter('filter_name', 'custom_filler_function')Code language: JavaScript (javascript)

So that ‘filter_name’ will pass through ‘custom_filler_function’ causing the data generated by the first of these to be modified by the second one.

Now we know what actions and filters are and what they do let’s proceed with creating our plugin.

We will register our ‘hello_wordpress()’ function to execute when the ‘admin_notices‘ action executes. The action prints notices on the admin screen. so we will be adding this line:

add_action( 'admin_notices', 'hello_wordpress' );Code language: JavaScript (javascript)

Here it is in the plugin file.

plugin file with the code

And here it is in an action on an actual page:

added action "HELLO WORDPRESS"

Let’s add more functionality but this time, let’s use a hook. Let’s create another function which adds a sentence to the end of every post.

function append_content($content) {
    return $content . '<p style="color:red;">' . 'This sentence is appended to the post\'s content' . '</p>';
}Code language: PHP (php)

Now let’s register it by adding this piece of code:

add_filter('the_content', 'append_content');Code language: JavaScript (javascript)

Here is what the PHP file now looks like:

file with the code

Here’s what the post looks like without the plugin activated

post without the plugin activated

And this is what it looks like when the plugin is activated

post with the plugin activated

That’s it. We’ve created and installed a working WordPress plugin.

Conclusion

We’ve shown how to create a functioning, custom made, basic WordPress plugin. ‘

All we needed to do was add the header comment, define custom functions, and then use them in conjunction with a WordPress Hook. It’s that simple to create a working plugin (although adding more features are another matter).

We hope that you’ve learned from this tutorial.

nv-author-image

Kristin Eitel

Leave a Reply

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