Search

The Beginner’s Guide to Creating a WordPress Plugin

Listen to this article

WisdmLabs-WordPress-Plugin-DeveloperSo, you’ve installed WordPress, gotten a hang of what happens behind the scenes, and are now excited about building your very first WordPress plugin?! You’re at the right place!

Building WordPress plugins is what most WordPress developers do, because plugins are at the core of the WordPress architecture.

I know you’re itching to get started with coding right away, but hold on a bit.

Jumping right into coding is not the right approach you should be taking. You’ve got to learn the basics first. The code can teach you the ‘how’s’ of plugin development. But the basics will teach you the ‘what’s’ of the plugin development process.

What is a WordPress Plugin?

“Plugins are ways to extend and add to the functionality that already exists in WordPress.”

That’s the easiest and simplest way to explain this.

A WordPress plugin is a piece of functionality that you can plug into your WordPress website, using hooks. Based on the functionality you’re looking to provide, you will need to use the appropriate hooks. Owing to this plugin architecture, the WordPress core continues to be lightweight and flexible.

Plugins on your WordPress website are located under the /wp-content/plugins folder. When you create a plugin, you’ll need to add it in the plugins folder or upload it using the WordPress admin panel. When WordPress is loaded, it loads only the activated plugins. The list of active plugins is stored in the database in the wp_options table.

How to Create a Plugin in WordPress

If you are new to coding or not familiar with plugin development, we recommend hiring an expert WordPress Plugin Development agency to plan, develop, execute and test your plugin.

Let’s get our hands dirty by writing some code. Any plugin you create needs at least one PHP file. This file can be placed directly inside the plugins folder, or in a separate folder (to maintain a cleaner structure).

So, let’s say you want to create a plugin ‘WDM Yoffa’ (you have to give it a unique name). You’ll need to create a PHP file say ‘wdm-yoffa.php‘ and place it under /wp-content/plugins/wdm-yoffa/

To begin with, you’ll have to add the following headers, so that users would know the plugin name, and its purpose.

<?php
/*
* Plugin Name: WDM Yoffa
* Plugin URI: //wisdmlabs.com/blog/the-beginners-guide-to-creating-a-wordpress-plugin/
* Description: A plugin that's kick-ass and cool all-in-one.
* Version: 1.0.0
* Author: WisdmLabs
* Author URI: //wisdmlabs.com
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

(The minimum header required is the plugin name).

Once you’ve added the headers, your plugin is almost ready! Irrespective of the code you add in this file, there is one line you can’t ignore. This line has to be placed at the very beginning for security purposes:

if ( ! defined( 'ABSPATH' ) ) {
    // do not allow direct access
    exit;
}

Now, your plugin code could be a few lines which could be present in one file, or thousands of line which would have to be spread across several files. There is a recommended structure you would need to follow to place your plugin and resource files.

The Folder Structure

Your plugin’s main file (wdm-yoffa.php) is placed in your plugin’s root folder wdm-yoffa. To place additional files such as stylesheets or JavaScript files, it would be good to create additional folders as follows:

  1. /admin: Where files which contain functionality related to the admin panel can be placed.
  2. /css: (You know this) A place for your stylesheets.
  3. /images: Where images which your plugin will use can be placed.
  4. /includes: Libraries and classes used by the plugin can be place here.
  5. /js: JavaScript files go here.
  6. /settings: Files related to your plugin’s settings go here.

Naming Conventions

Along with the folder structure, it’s recommended to follow a naming standard, to avoid unnecessary conflicts with other plugin functions. So, along with naming your files with a prefix, it’s better to name your functions, variables, even style classes with a prefix.

The prefix could be a shortened version of the plugin name, or an acronym.

Hooking to Actions and Filters

For your plugin functionality to come into play based on WordPress or on how other plugins work, you’ll need to use hooks. For example, say you want to perform some operation when the theme is loaded (if you recall ‘How WordPress is Loaded‘, you’ll know active plugins are loaded before the theme). In this case, you can hook the functionality on after_setup_theme hook.

function wyf_do_something()
{
    /* do something */
}
add_action ( 'after_setup_theme', 'wyf_do_something' );

If your plugin is an add-on plugin for another WordPress plugin, you’ll want to write code dependent on the plugin’s activation and deactivation state.

A Note About Must Use Plugins

Must use plugins are special plugins placed in a completely different directory wp-content/mu-plugins and are automatically activated. You cannot see the plugins in the Plugins list in the admin panel. And must use plugins can be deactivated only be removing the plugin from the mu-plugins folder.

 

So, that’s about it from my side. These are the basics you need to know when you build your first WordPress plugin. For an in-depth guide, you can refer to the Plugin Handbook, which explains several plugin development processes in detail.

If you have any questions up till now, you can direct them to me using the comment section below.

Until next time, happy weekend! 🙂

Harish Kashyap

Harish Kashyap

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

How to Make Responsive Tables using CSS without Tag
Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads