Cycle with WordPress wheels

How to create a WordPress plugin

In this post, you will learn how to create a simple WordPress plugin without any complex setup or need of FTP/SSH to your hosting server.

Before we begin with this post, let us understand a few things.

I will try to keep the code as simple as possible. My objective in this post is to help you understand how easy it is to create WordPress plugins.

You do not need any FTP or SSH access to your hosting server. All you need is access to your WordPress dashboard with an administrator account with the ability to upload new plugins.

No tools setup is required for creating this plugin. You can write the code in a basic text editor like Notepad which is already included in your Windows computer.

However, I would recommend using a slightly better text editor like Notepad++ which also offers syntax highlighting, but it is completely optional.

No FTP tool is required to upload the code. If you have one and know how to upload, you can try that, but it is not needed at all.

WordPress is amazing

WordPress offers amazing features right out of the box. If you want to create a blog using WordPress, it is as easy as installing software on your Windows computer. There is no coding required.

WordPress makes it so simple. It is a complete blogging system with everything in place to write your next awesome blog.

Let us take a look at some of the basic features of WordPress supported out of the box-

  • Create posts, classify them with categories and tags
  • Upload and manage images, videos, etc. using its built-in media library
  • Define the URL structure of your choice using permalink settings
  • A simple yet fully functional comment system with dedicated comment management dashboard
  • Thousands of free themes to change your blog’s appearance with just a single click
  • Thousands of free plugins to add any additional functionality

There are many more features that I cannot list here, but you got the idea. WordPress is a complete powerhouse for bloggers helping them focus on their content rather than worrying about managing the technicalities.

Why plugins

No matter how comprehensive a particular software is, there may be innumerable cases where some additional functionality is required. The same is the case with WordPress. Though most of the blogging features are included in the core, there may be situations where additional functionality is needed. That’s where WordPress plugins come into the picture.

For example, if you need to add your copyright text in the page footer, you can find and install a plugin that offers that functionality. The plugin may also add settings page to your WordPress dashboard to allow you to configure the plugin settings.

But, what if the plugin you found includes 10 more features which you do not need. Though it will work for you, it will add bloated code to your blog by adding unnecessary additional features which are of no use to you.

In such a scenario, a good option can be to create your plugin. Yes, it is very easy and a much better option if you want to add a small functionality to your WordPress blog.

Before we dive into creating the plugin, there is one thing you need to know- WordPress Hooks.

What are WordPress Hooks

As the name suggests, hooks are small checkpoints within the WordPress code where you can inject your functionality. You can just write a function and connect to the hook and your function will be called every time the hook is executed by WordPress.

For example, WordPress defines a hook called wp_footer‘ which is called by WordPress when it renders the page footer. You can connect your code with this hook to print your copyright notice in the footer.

Hooks are of two types-

  1. Action hooks
  2. Filter hooks

Action Hooks

Action hooks are mainly for executing some code and print it. For example, I already told you about wp_footer. It is called during the creation of page footer during page load and you can print anything you want to the footer by hooking your function to this action hook.

You can also use an action hook to execute some code that has to be executed on each page load. For example, wp_init is an action hook that is executed when WordPress loads the required data before rendering the page. You can hook into this to execute anything you need before the page is sent back to the user.

All you have to do is create a function and tell WordPress to call it whenever wp_footer is executed.

Filter Hooks

Filter hooks are useful if you want to manipulate or modify data within the WordPress functions. For example, the_content is a filter hook that filters the post content to be printed in the post or page. You can hook into this filter, and it will pass the content string to your function. You can modify this value and return the new value which will be taken by WordPress to print the final content on the page.

This understanding is enough to create your first plugin. I will discuss the code in the next section.

The Plugin

We have got a good understanding of WordPress hooks. Now, using this knowledge, we will create a small plugin. To keep things simple, our plugin will simply add some text to the footer of our WordPress blog.

A WordPress plugin can be a single file or it can be a directory with a bunch of files. To keep things simple, our current plugin will be of a single page, with all the code in the same file.

First of all, we need to select a unique name for our plugin file which must not be a clash with the name of any other plugin in the WordPress repository. To ensure that there is no clash, we will prefix our plugin name with our blog’s name. A good file name can be webrosis-footer-printer.php.

You can use the prefix of your name, blog or company. The file name is completely your choice.

Create a file with this name and open it in a text editor.

Every WordPress plugin index file (in case of a directory) or main file (in case of a single-page plugin) must contain header comment with specific header fields as defined by WordPress.

WordPress uses the details provided in this header comment to display plugin’s information in the installed plugins list in the WordPress dashboard.

In the header comment, it is mandatory to define "Plugin name". Everything else is completely optional. As an example, below is a header comment which I have created for our plugin. You can replace the values with your own.

<?php
/**
 * Plugin Name:       Webrosis Footer Printer
 * Description:       Prints custom content in footer
 * Version:           1.0
 * Author:            Abhineet Mittal
 */

There can be more header fields, but for now, this seems enough for our plugin.

The Code

We want to print our custom content to page footer. So, we will hook into ‘wp_footer’ action hook.

We will first write a function to print our custom content and then hook it to wp_footer action through `add_action’ function.

// Function to print copyright notice in footer
function webrosis_print_content_in_footer() {
    echo 'The content on this website is copyright protected. Do not copy. All Rights Reserved.';
}
// Now hook this function to wp_footer action
add_action( 'wp_footer', 'webrosis_print_content_in_footer' );

Save the file.

That’s it. This is the only code you need to write. When added to your WordPress blog, this plugin will print the given content in the page footer.

Here is the complete code-

<?php
/**
 * Plugin Name:       Webrosis Footer Printer
 * Description:       Prints custom content in footer
 * Version:           1.0
 * Author:            Abhineet Mittal
 */

// Function to print copyright notice in footer
function webrosis_print_content_in_footer() {
    echo 'The content on this website is copyright protected. Do not copy. All Rights Reserved.';
}
// Now hook this function to wp_footer action
add_action( 'wp_footer', 'webrosis_print_content_in_footer' );

This plugin will not add any configuration settings to your WordPress dashboard as we have not coded that. However, once you start writing plugins, you can learn about the WordPress Settings API to add your custom settings page in the WordPress admin dashboard.

Installation

You have created your first plugin. Now its time to add it to your WordPress blog. Use the following steps-

  1. Zip this plugin file
  2. Go to your WordPress blog dashboard and click on "Plugins > Add New"
  3. Upload the zipped file of your plugin
  4. Now activate the plugin by clicking on "Activate"

If you have done everything till now exactly as I told you, the plugin will be successfully activated.

Now go to any post or page on your blog frontend and you will find that your content has been added to the footer.

Congratulations! You have successfully created your first WordPress plugin.

So, you saw how amazingly simple it is to create a WordPress plugin. If you want to add small functionalities, it is better to write your small functions and add them to your plugin.

Your plugin can only be installed by uploading the zipped file. It will not be available in the WordPress plugins repository until you submit it.

I have not covered uploading the plugin in the WordPress repository as it is a separate process and requires many more steps. At this level, you must first practice writing plugins. Once you start writing useful plugins, you may consider submitting them to the WordPress plugins repository to be used by the community.

Way forward

In this post, you have learned how to create a WordPress plugin. But our example was a very simple one as it was just to explain to you how a plugin works.

To create more meaningful plugins, you can read about other filters and API functions provided by WordPress. A good starting point is WordPress Documentation.

Here are a few simple ideas which you can implement in your plugin-

  • Add Google Analytics code by hooking into wp_head action
  • Add beautiful author box below the content of your posts by hooking into the_content filter
  • Modify the title of your pages by hooking into ‘wp_title’ filter

You can think of many more ideas as per your blogging requirements.

Conclusion

We created a very simple WordPress plugin that printed some custom content in our blog’s footer. It showed, how easy it is to add small functionalities to your WordPress blog by adding simple code in the form of plugins.

Thanks for reading. I hope you found this post useful.

Share this:

Leave a Comment

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