How to create a WordPress Plugin

Want to become an expert in Python 3 and Django 3?

Don’t Miss the #TwitterFiles!

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!

Andrey Bulezyuk
Andrey Bulezyuk

Andrey Bulezyuk is a Lead AI Engineer and Author of best-selling books such as „Algorithmic Trading“, „Django 3 for Beginners“, „#TwitterFiles“. Andrey Bulezyuk is giving speeches on, he is coaching Dev-Teams across Europe on topics like Frontend, Backend, Cloud and AI Development.

Protocol Wars

Understanding the Key Players: Ethernet, Wi-Fi, Bluetooth, and Zigbee The Invisible Battles: How Data Streams Clash in the Airwaves Adapting to an Evolving Tech Landscape: New Contenders and Challenges User Empowerment: How Our Choices Determine the Winning Protocol...

Google Earth 3D Models Now Available as Open Standard (GlTF)

Unleashing the Power of 3D: A Comprehensive Guide to Google Earth's GlTF Models From Virtual to Reality: How to Utilize Google Earth's GlTF Models for Your Projects Breaking Down the Barriers: The Impact of Open Access to Google Earth's 3D Models on the IT Industry...

When you lose the ability to write, you also lose some of your ability to think

Reviving the Creative Process: How to Overcome Writer's Block in IT Staying Sharp: Techniques for Keeping Your Mind Active in the Tech World From Pen to Keyboard: Transitioning Your Writing Skills to the Digital Age Collaboration and Communication: The Importance of...

Reverse engineering Dell iDRAC to get rid of GPU throttling

Understanding Dell iDRAC: An Overview of Integrated Remote Access Controller Breaking Down the Barriers: How to Disable iDRAC GPU Throttling for Maximum Performance Optimizing Your Dell Server: Tips and Tricks for GPU Throttle-Free Operation Maintaining Stability and...

0 Comments