WordPress is a great platform with tons of features and options. However, there are some things that it doesn’t have by default. This is where a custom plugin comes in.

Custom WordPress plugin will allow you to add new functionality to your website or change the existing functionality, it’s also another way to load custom scripts, change styles and manipulate the WordPress website with some JavaScript.

Creating a WordPress plugin is not as difficult as it sounds. In fact, it’s quite easy! All you need is a code text editor such as Visual Studio Code, and some basic knowledge of PHP.

You can also download the starter custom plugin from my GitHub repository with code that I use on projects where a page builder is involved such as Oxygen Builder or Breakdance, but I do recommend following some steps below to create your first custom plugin as the custom plugin in Github has a lot more functionality that you may not need.

Benefits to create a custom WordPress plugin?

Add New Features & Functionality

Add new features, and functionality and modify WordPress the way you need it. No need to rely on free third-party plugins from WordPress plugin repository that may need to be updated regularly to keep them functional and secure.

Organised Code

When you develop your own plugin, you have the freedom to organise your code. Read more on WordPress codex about best practices and keeping your code orgonised, this will make your life easier and other developers life easier if they ever have to tinker with the plugin.

This makes your code more readable and maintainable.

Alternatively, you can use plugins such as Code Snippets instead of a custom plugin, but I still feel as a developer you can organise your code better with your own plugin, and call different templates from different locations to expand your own plugin and the functionality of WordPress.

Can be installed on multiple websites

If you develop a plugin that could be useful for other WordPress websites, you can easily install it on multiple websites that you are going to develop or manage.

Better Performance

Your code is more organised and optimised, it puts less load on the server, making your website faster, as it does not carry the extra load that may impact the website performance.

Creating WordPress Plugin

The first thing you need to do is create a new folder inside the /wp-content/plugins/ directory. You can name this folder anything you want. I am going to call it my-custom-plugin.

If you have WordPress set up locally it should be easy enough for you to navigate to the folder, if it’s on the server then you will need to connect to it via FTP or SFTP. I recommend using Local to work on your WordPress website, which is a free tool, and creating a website with local is super quick and easy!

After you create the folder, inside it, create a file and name it my-custom-plugin.php. The plugin file must have the same name as the folder in which it resides.

Once you have created the plugin file, open it in your text editor and add the following code to it:

<?php
/**
* Plugin Name: My Custom Plugin
*/
?>

That’s it, now you can go to your WordPress dashboard, and activate your “My Custom Plugin”. The above is your header information for the plugin which is required to work.

You can also add more information to the header that will explain the plugin better, see below:

<?php
/**
* Plugin Name: My Custom Plugin
* Plugin URI: https://example.com/plugins/the-basics/
* Description: Custom functionality for my WordPress website.
* Version: 1.0
* Requires at least: 5.2
* Requires PHP: 7.3
* Author: John Doe
* Author URI: https://author.example.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://example.com/my-plugin/
* Text Domain: my-custom-plugin
* Domain Path: /languages
*/
?>

et’s break down this code:

  • Plugin Name: This is the name of your plugin. It is required, and it should be unique.
  • Plugin URI: The URL to where your plugin is located.
  • Description: A short description of what your plugin does.
  • Version: The current version number of your plugin e.g. 1.0
  • Requires at least: The minimum version of WordPress required for your plugin to work.
  • Requires PHP: The minimum version of PHP required for your plugin to work.
  • Author: Your name.
  • Author URI: Your website address.
  • License: Read more on the GNU Website.

Now that we have the basic structure, let’s add some functionality!

We will start by creating a function that will output “Hello World!” when called. The code for this will look like this:

function my_custom_plugin_function(){
    return "Hello World!";
}

This function can be called anywhere on your website using the following code:

<?= my_custom_plugin_function(); ?>;

And that’s it! You’ve created a basic WordPress plugin. Of course, there is a lot more you can do with your custom plugin, but this is a good starting point.

You can now add snippets, and functions to your custom plugin.

Download the starter custom plugin from my GitHub repository where functions are already written to load scripts, styles and add PHP files inside the includes folder which will load automatically.