You are a developer? Join our network of volunteering Developers!

How to create a WordPress Plugin

Introduction

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
*/

Here, only 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.

Plugin Structure

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.

Naming Convention

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 ak_commonFunction.

Helper Hooks

WordPress offers three hooks for custom plugins.

register_activation_hook()

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.

register_deactivation_hook()

This hook when your plugin is deactivated. You can use this hook to un-load dependencies, remove settings etc.

register_uninstall_hook()

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.

Wrap Up

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!

Serving Machine Learning Model with MLFlow

We are going to briefly follow the Quickstart in the official MLFlow Documentation. We will create, serve and invoke a Machine Learning Model with MLFlow. Installation pip3 install mlflow Create a new folder for our little project and create a new file called...

Laravel 6 – Build Your First Laravel Application

Introduction Laravel is a powerful MVC PHP framework. From official docs "Laravel is a web application framework with expressive, elegant syntax". You must've heard about Laravel if you've ever worked with PHP. Laravel has experienced major growth since it's initial...

MLFlow vs. Metaflow

We compare two Machine Learning and Data Science frameworks - MLFlow vs. Metaflow. These Data Science and Machine Learning Frameworks are the most popular in their category - ML. They provide you a fixed set or best practices, methods, classed and helping tools (like...

Introduction to Ruby on Rails

Learn why Ruby on Rails can help speed up the development process. and why so many big companies rely on Ruby on Rails to run their business.

Serve Keras Model with Flask REST API

This tutorial will briefly discuss the benefits of serving a trained Machine Learning Model with an API. Then we will take a look at a precise example using a Keras Model and Python Flask to serve the model. You'll learn how to Serve Keras Model with Flask REST API....

How to create a WordPress Plugin

Introduction 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 &...

Getting started with PHP 7 in 2020

What is PHP? PHP stands for Hypertext Preprocessor (earlier called "Personal home page"). It is an open source scripting language created for the web development. PHP is a server-side language which means all the code is rendered on server and only static HTML, CSS...

Complete Vue.js 3 Guide

This is the first and most comprehensive Guide to Vue.js 3. We give an overview of the Changes, an introduction in usage of VueJS3 with Example Codes.

IT Career Path Nobody Is Discussing

Whatever path you decide on, we will help you find out the ideal things to do to take. Your career path contains the jobs you should hit your final career objective. While the career path will appear different for everybody, aiming to acquire equal parts experience in...

Awesome Future of Frontend in 2020

Before we dive in to Future of Frontend in 2020. Let's begin with some basics as we gradually get more forward-looking. We obviously cannot know what technology will dominate in 2020 and what framework will pop up. But we can try at least. What is frontend? Frontend...