What is WordPress? A technical definition is “WordPress is an open source content management system (CRM) built in PHP”. Currently WordPress is the most popular CRM available for free! If you’ve ever used WordPress, you definitely know about themes & plugins in WordPress! In this article I will show you how to create a simple WordPress plugin in less than 15 minutes! But first, let’s talk more about what a plugin actually is.
Take a look at our fresh PHP 7 Introduction.
What is a WordPress Plugin?
WordPress plugin is a package of PHP script(s) that can alter your website. A plugin can alter your website in many ways i.e. From adding a simple message in header to creating a whole shop on your website eg. WooCommerce. Plugins can also modify/tweak existing features of your website like changing Login, sending email on specific event etc.
WordPress theme or Plugin?
If you have ever opened a WordPress theme, you’ll know that every theme contains a
function.php file. This file contains all the theme related logic and any functionality provided by a particular theme. You can modify this file and add your own custom functionality too. So if we can directly add new feature in
function.php, why do we need a plugin? Well the answer is simple, if you only want to add some text in the header or something small like that, you can surely use theme’s
function.php. If you want something like a custom chat bot or anything like that, then a plugin would better suit your needs.
The main difference between a theme’s ‘function.php’ and separate plugin is that plugin’s functionality persists regardless of your website’s current theme but any changes from
function.php will only be applicable when that particular theme is in use.
Creating our first WordPress Plugin
WordPress plugin’s default structure only requires one PHP file in a separate directory with a PHP block comment containing various data about that plugin. So, to create a plugin you need to navigate to your
wp-content/plugins folder. Create a new folder name
myFirstPlugin. Inside this new folder create a new file named
myfirstplugin.php. Open the created file in any text editor of your choice and paste the following in it.
<?php /* Plugin Name: My First Plugin Plugin URI: http://mywebsite.com/ description: My first ever WordPress plugin Version: 1.0 Author: Arman Khan Author URI: https://instagram.com/codingwitharman License: GPL2 */
Plugin Name is required, but it’s a good practice to provide as much data as possible about our plugin.
That’s it!! You’ve created your first ever WordPress plugin. You can log in to your
wp-admin and go to
plugins and active this plugin. Of course, this plugin does not do anything, yet! But it is an active plugin.
How to create a great plugin?
There are some parameters that you should consider while creating your plugin. These parameters will help you create a successful well documented plugin.
How to structure your plugin? Well, this question is quite common among new WordPress developers.
If your plugin provides complex functionality, you should divide it in multiple files. For example, if your plugin has a main class, you should put that in your plugin’s main file and provide connections to other files through it. If your plugin includes UI related files, you should consider sorting them in separate folders like JS/CSS/Images and put all these folders in one assets folder. Well of course this is not a requirement from WordPress but a good plugin structure is always preferred compared to a bad one.
When creating functions in the plugin, you should be very careful naming them. You should never choose more general names for your functions as it might clash with other plugins that have similar functionalities.
The most common solution is to use unique prefix. For example, if your function name is
commonFunction then you should replace it with something like
WordPress offers three hooks for custom plugins.
This hook allows you to create a function that runs when your plugin is activated. You can use this hook to load dependencies, check for plugin version or check for WordPress or PHP version.
This hook when your plugin is deactivated. You can use this hook to un-load dependencies, remove settings etc.
This function runs when a WordPress administrator delete your plugin from backend. This is a great way to remove plugin specific data like tables in database or custom field created by your plugin.
Well, so now you know how to create a simple WordPress plugin. Now you can easily follow articles that require you to create WordPress plugin first. Of course this guide does not contain advance features that a plugin can provide, we’ll cover those topics in future articles for sure. Stay tuned!