One of the best and most powerful features of WordPress is the ability for anyone to create custom code and organize it in plugins. The code can be about anything and can infinitely extend a site’s capabilities.

In my experience, one of the best uses of a custom plugin is organizing the additional code that any custom-developed site needs. Many of the tutorials you can find on the internet include small snippets of code for something highly specific, and the recommendation is usually to add this code to the “functions.php.” Although this is a valid suggestion over time, it leads to a bloated file filled with all sorts of small stuff that was needed at some point. Other than bloat and general messiness you will face the issue of moving the entire functions.php content if you need to change the theme – this is not pretty, speaking from experience. 

The best thing is to organize everything in a small plugin for that project specifically. 

What do you need to create a Custom Plugin?

Access to the site codebase

Although having FTP access to the code base is very useful, it is not required. You can write the code locally, archive it in a zip file and upload it to the site via the upload plugin functionality.

This will also serve as a way of updating the plugin unless you update directly in the files – this is never a good idea in a production environment.

Create the Plugin folder

For the purpose of this tutorial, we will create a simple plugin for the Words or Old site that will include a CSS file to the header. Assuming that you will create the plugin using direct FTP access, you will need to create a new folder in the “wp-content > plugins” folder.

The name of the folder should be something that you will easily understand. For example “wold-tools”, where “wold” is an abbreviation of Words of Old.

Create a main plugin file

The next step is to create the main file for our custom plugin. That file should have the exact same name as the folder and should be .PHP file.

As you can see, I have added a couple more folders. Those are optional and not required, but I like to have them for organizational purposes.

In the wold-tools.php file, we need to add some declarations about the plugin. Those take the form of PHP comments in a specific format:

<?php
 
/*
 
Plugin Name: Words of Old Custom tools
   
Description: Pluging containing custom features for the Words of Old site
 
Version: 1.0
 
Author: Vasil Georgiev
 
Author URI: https://worldofold.com
 
License: GPLv2 or later
 
Text Domain: wold

Domain Path: /languages
 
*/

List of the base plugin attributes:

  1. Plugin Name – The name of your plugin. This will be seen on the plugins page in the admin panel of WordPress

  2. Description – Short description of the plugin
  3. Version – The current version of the plugin. If you intend to update the plugin, you should increment that version every time you add new functionality. The dot in the number serves as a mind divider – the major versions are on the left, and the minor on the right. The difference between a major and minor version is usually found in the amount of functionality. For example, fixing a few bugs deserves only a minor version bump, whereas adding new functionality that will change a lot the behaviour of the site deserves a major version update. It is up to you how you will increment the versions. 
  4. Author – The author of the plugin. It can be a company or person’s name.
  5. Author URI – The site of the author.
  6. License – The licence terms of your plugin. Not required.
  7. Text Domain – the text domain that you will use for the plugin. This is to inform translation plugins what the plugin text-domain is.
  8. Domain Path – this is the path for the translations file if such exists

After you fill in all this and save the file, you will have a functional plugin, even if it won’t do a thing. 

You can check on the plugins page, and you will see something like this:

 

Adding some functionality to the plugin

As I said earlier, we will use this plugin to add a CSS file to our theme.

First, we need to create the CSS file and add it to our “assets” folder. Let’s call it “wold-global.css”.

The folder structure might look something like this:

Next, we must enqueue the file in the theme using the “wp_enqueue_scripts” WordPress action. This code will be added to the “wold-tools.php” file.

add_action('wp_enqueue_scripts', function() {	

	wp_enqueue_style("wold-global", plugin_dir_url( __FILE__ ) . 'assets/wold-global.css',[], "1.0.0");	
	
}, 11 );

You can read more about the “wp_enqueue_scripts” hook in the official WordPress documentation.

Voila, now you have a functional plugin that adds custom CSS to the theme. Remember to update all the names and strings with something relevant to you.

Conclusion

Custom WordPress Plugins are useful tools, and you should use them whenever you need to extend your site functionality. I have made a tools plugin for all sites I have developed in the past 4-5 years, and those contain from some small stuff like CSS and extra info here and there, to custom integrations with 3rd party providers. 

Don`t feel intimidated by the idea of writing your own plugin. Start small and go from there.