How To Write Your First WordPress Plugin

After you have been using WordPress for some time, I bet you have some slight modifications you want to make. Maybe changing the title of a specific archive from “Category: Favorites” to “My Favorite Posts”?

I’m going to walk you through the basics of how to write your first plugin so you can modify your site or write a plugin to help out others who may not want to get involved with code.

Basic File Structure

Every WordPress plugin starts out with at least one file. It handles all the functionality required and controls the files that are included. This file should be named similar to what you’re calling your plugin, but it does not necessarily have to match word for word. (an example would be Hello Dolly and hello.php–see? Similar)

When WordPress boots up, it loads it’s core files first, then starts to walk through the plugin list. It includes each plugin in sequence until it reaches the end. This is where your plugin gets included and adds functionality by running code contained in this main plugin file.

Note: This is why you get the “White Screen Of Death” with WordPress. When there’s an error, processing the code stops and you’re left with a white screen.

The Plugin Header

Within the main plugin file, WordPress looks for a specific PHP comment that defines the information needed to show the plugin in the backend. It looks a little something like this (which you can find in the WordPress documentation by the way):

 * Plugin Name: Plugin
 * Plugin URI:
 * Description: Basic WordPress Plugin Header Comment
 * Version:     20160911
 * Author:
 * Author URI:
 * License:     GPL2
 * License URI:
 * Text Domain: wporg
 * Domain Path: /languages

Each line references a different aspect of the plugin:

  • Plugin Name: What are you calling this plugin?
  • Plugin URI: this link is where you will send users when they click on the plugin “visit site” in the plugin screen of the admin
  • Description: What does your plugin do for the user?
  • Version: What version is your plugin? Usually, when first starting the plugin, you’ll want to set this to version “1.0”
  • Author: Your Name
  • Author URI: Just like the Plugin URI, where should users be sent when they click on your name? Your website perhaps?
  • Text Domain: This is a setting that WordPress uses for Internationalization (or I18n as you may have seen ), but we won’t be using that and you can just leave it out for now.
  • License: This ( & License URI) you can grab from the WordPress docs as all plugins have to be compatible with WordPress’ licensing.

Main Plugin File

This is the file that you’re going to be writing the “meat” of your plugin. It’s essentially your entry point into the WordPress lifecycle.

If your plugin is adding some simple functionality it might fit all in this single file, however, if you have more than about 100 lines, I’d consider breaking it up into separate files and including them. Breaking up the code into multiple included files helps with maintenance and readability.

Probably one of the biggest concepts to understand when writing a plugin is how the code is run. In PHP, you have functions that contain a section of code that runs whenever you call it. In WordPress, you have specific helper functions called actions and filters.

These actions and filters are hooks into the WordPress loading cycle so you can have functions run at specific times in the cycle. Also, actions can be seen as something that does something, and filters are filtering something.

For example, let’s say that you wanted to send an email every time WordPress loads before it does anything else (this is a terrible idea, it should never be done). You would write the function to send the email (wordpress_loaded()) then you would use the add_action function to call your function.

// Define our function to run when WordPress loads.
function wordpress_loaded() {
   wp_mail(, "WordPress Loaded!" );

// Add the hook to call our function when WordPress begins to load.
add_action('init', 'wordpress_loaded');

Also, if you wanted to change one of your act archive page titles, you would use the add filter function, grab one of the parameters and do any checks to make sure you’re on the right page, then return what the title needs to be.

function rename_archive_title( $title ) {
    if ( is_archive('posts') ) {
        return "My New Archive Title";
    return $title;

add_filter( 'get_the_archive_title', 'rename_archive_title' );

Activation & Deactivation Hooks

Since you now know about add_action and add_filter, WordPress includes some addiotional hooks like activation and deactivation. These run any time a user activates or deactivates your plugin on their site.

Typically people use these to clean up anything the plugin is using.

Plugins can use things like transients and custom database tables that might be left behind after the plugin has been removed. Although, not everything should be removed on deactivation. You want all the users’ settings to stay, but you want some data to be cleaned up.

WordPress also has an uninstall hook that you can use to remove everything that the plugin would leave behind. These hooks are great to set up in the beginning and you just keep adding to them as your plugin grows. Everything you create that could get left behind, should be added to this uninstall hook.

Installation & Testing

Once you have finished programming your plugin and need it to actually run on a WordPress site for testing, all you have to do is package up the file into a .zip. If you’re on a Mac, control-click and select compress. on a pc …. something.

You can then upload this zip to your WordPress site and activate it. Although I would highly recommend testing it locally before uploading it to your live site as it could cause some headaches if you didn’t double check your code first.

To Sum Up

You should now understand how WordPress loads plugins, how to write a basic plugin, add functionality to WordPress and some best practices for cleaning up after your plugin.

Let me know if you got some value out of this in the comments. I’d love to hear about your plugins that you’re building!

More Resources

How To Back Up Forge WordPress Sites to Amazon S3

I’ve had my personal WordPress site and all of my clients’ sites hosted with Laravel Forge for a little over a year now. If you haven’t heard of Forge yet, you definitely need to check it out. Yes, it is mainly for PHP applications, but it will also host WordPress sites like a champ. Forge is great, but the one thing it lacks is a way to back up your sites.

I wrote a script to handle backing up each WordPress site to Amazon S3 for safe-keeping as a part of my own backup plan.

Continue reading How To Back Up Forge WordPress Sites to Amazon S3

How To Develop A Backup Plan For WordPress

Backups are an essential part of any website. What would happen if you woke up one morning and someone hacked your site? Or worse, your web host had dissolved and no longer existed? I’ve heard many stories of people that didn’t think a backup plan was important until it was too late. Continue reading How To Develop A Backup Plan For WordPress

Efficiently Deleting WordPress Posts Using SQL

For the past few weeks I’ve been working on a WordPress plugin for importing a CSV into a few custom post types and taxonomies. I realized that sometimes going “The WordPress Way” isn’t actually the best way to do things. Occasionally, the quickest way to get stuff done is to dive into some SQL.

Continue reading Efficiently Deleting WordPress Posts Using SQL

Why Every Business Should Have A Blog

How many different blogs do you read in a day ( if you’re anything like me, you read a ton of blog posts before you even roll out of bed). Often they are just interesting posts that friends post on Facebook and Twitter but nonetheless, I read tons of blog posts and you probably do too. Your goal as a business is to provide value to the customer. One of the ways you can build value is by giving out free information through a blog.

Continue reading Why Every Business Should Have A Blog

How to Add Google Analytics to WordPress

Google Analytics is one of the best ways that I have found to really track who is visiting your website. It gives you a clear view how users are getting to your site, how they interact with it, and how long they stay on it. This is one of the most important tools you need if you plan on doing any online marketing. After all, what is the point of an online presence if you don’t intend to use it?

Continue reading How to Add Google Analytics to WordPress