Create Your First WordPress Plugin – File Structure Tutorial

When you are ready to create and begin programming your first WordPress plugin, the first thing you need to worry about is the file structure of the plugin.

Name Your WP Plugin

To get started, you will need to name your plugin. This should be a unique name that is not used by any other plugin in existence! In fact, it is extremely important that you use a name that you are very confident has never been used before and will never be used.

In many cases, you can just use the full name of the plugin. However, if your plugin name consists of common words, you will need to exercise more care to ensure you are using a unique name.

Create The Plugin Directory & Your First Plugin File

For the purpose of this tutorial, I am going to use a fictional plugin name to show how this works. In this tutorial, I will be creating a plugin named "rsplugins".

The first thing you will need for your plugin is a directory folder. This folder should have the same name as your plugin, so I'll be creating a folder named "rsplugins".

I recommend creating a special directory on your own computer that will contain your main plugin folders to keep them neatly organized. I have a folder called "WP Plugins" on my Desktop. Within that folder are more folders for each plugin I have created. Within each individual plugin folder, I then create a folder to specify the version number. Then, the actual plugin folder "rsplugins" will be located within the version number folder.

To show you what I am talking about, here is the directory structure I have created:
WP Plugins >> ryanstevensonplugins.com >> 1-0-0 >> rsplugins

In this example, ryanstevensonplugins.com is the actual domain name of the plugin I'm creating. "1-0-0" is the first version number.

I use a three number system for the version numbers to make it easier to understand. The first number is the main version number (ie, version 1). I increment the main version number when I make major changes or additions to indicate a new edition. The second number is a secondary version number. I increment this number to signify that features have been added, although not enough to really call it a new edition. The last number is incremented when a bug fix is released, so it indicates that there are not new features but is still an important update.

This means that the "rsplugins" folder is the actual plugin folder. Everything else is merely for organization. It may not seem like a big deal at first, but when you have many different versions and many different plugins, you will be thankful that you kept your files organized.

Main WP Plugin File

Now it is time to create the main file. This file must be a php file and it must have the same name as the folder.

For this example, I will create a file called "rsplugins.php", which should go in the "rsplugins" folder we just made.
rsplugins >> rsplugins.php

Main Plugin File Code

Now it is time to develop some code in your first file!

Before you get started with the main coding, there is some required script that you must have at the beginning of this file.

At first, this may not appear like normal code, so you may think that it is not needed. However, WordPress will not recognize your plugin without this code, so it is actually extremely important!

One more important thing to note: This code is only needed in the main file and not additional files that you may create later. This code tells WordPress the basic information for your software, so if you use it again in other files, WP will think you have many different plugins (which would be bad).

Here is the initial, required script. In this tutorial, I'll be putting this at the top of my "rsplugins.php" file.

<?php
/*
Plugin Name: Ryan Stevenson Plugin Development Tutorial
Plugin URI: http://ryanstevensonplugins.com/
Description: An example WP plugin for this tutorial.
Version: 1.0.0
Author: Ryan Stevenson
Author URI: http://ryanstevensonplugins.com/
License: Single Owner License - Not for Resale, Transfer or any kind of Distribution
*/
?>

Obviously, you will want to change the information in this code for your own site. Nearly all of this information is displayed in the WP plugins list, so your users will see it.

Plugin Header Code Explanation:

Plugin Name - This should be the full name of your product (does not have to match the name you have used for your folder and main file). This is displayed in the WP plugins list to your users.
Plugin URI - The website address for your product. This link is also shown in the WP list.
Description - Also shown in the plugin list, this is a long description of your software (there does not seem to be a hard limit to the amount of text that can be used here and you can also include some basic HTML here as well).
Version - The current version number. This should be changed each time you release new code (I talked about version numbers previously for file organization and folder structure creation).
Author & Author URI - This is also displayed in WP. Your name is turned into a link, with the URI being the destination for that link. This allows you to provide a second website for your users. If you have a main website for your businesses, use that here to gain some extra exposure for your other plugins.
License - With free plugins in the WP repository, you will want to be sure to use the proper open-source licensing required by WordPress. For commercial plugins, your licensing may vary. I typically license my plugins to a single owner, while allowing them to use it on as many of their own domains as they want.

That is all of the basics to get started creating your first WordPress plugin. You can then begin programming code in your main plugin file, although I personally use this file to include the other files that I'll be using. Take a look at my other plugin development tutorials for more information on what to do next.

Comments are closed.