How to Write Your First WordPress Plugin

How to Write Your First WordPress Plugin

Thanks to the WordPress community there are fantastic plugins to extend WordPress, and you can add to this. Not only will this expand your knowledge of WordPress, if your plugin fills a gap in the market, you could earn money from it.

We’ll assume a good knowledge of WordPress; should you need a helping hand with PHP, look at the W3Schools PHP Tutorials.

Start With a File

First of all put a PHP file in the modules folder. Give it an appropriate name that is specific, searching for clashes.

The base file has to start with a specially formatted comment to ensure WordPress knows it’s a plugin, and should look like this:












Plugin Name: Your Plugin Name Here

Plugin URI:

Version: Current Version Number

Author: Your Name Here

Description: What does your plugin do?



Give Your Plugin a Hook

 To make sure functions are carried out at appropriate times, use a WordPress hook. These look out for functions needing to run, then run them at the right time.

There are two type of hook: Actions and Filters.

An Action executes your code at a certain point. A Filter will receive a value, and depending upon the code, may alter it before returning it. See which hooks are available at Adam Brown’s WordPress Hooks Database.

Manipulate the Database

Most plugins, unless basic, modify the database. There are several ways to do this:

The Option Mechanism lets you saved pieces of named data as strings, arrays or PHP objects. Choose unique names and try to limit the amount you use, remembering you can use an array. It needs to be data related to the setup and isn’t expected to change regularly.

Creating a new option:

1 add_option($name, $value)

Retrieving an option:

1 get_option($option)

Updating an option:

1 update_option($name, $value)


Custom Taxonomies allow you to put objects into groups, such as different kinds of post receiving different actions could be placed together.

Post Mata Custom Fields enable you to change data for specific posts and pages, ie:

1 <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?>

You can also create a custom database table, an advanced flexible method to take your plugin to the next level.

Activating Your Plugin

To activate (and deactivate) your plugin use hooks again:
















register_activation_hook(__FILE__, ‘your_plugins_name_activation’);


register_deactivation_hook(__FILE__, ‘your_plugins_name_deactivation’);


function your_plugins_name_activation() {


// Add code here for things your plugin should do on first activation




function your_plugins_name_deactivation() {


// Add code here that runs once on deactivation



Uninstalling Your Plugin 

Hopefully you’ll never want to do this, but should the worst happen, here are two ways to go about uninstalling:

Put all necessary code into a file name uninstall.php.


Use a hook within the plugin – a function containing the code to be run once on uninstallation.

No uninstall hooks in your plugin will run if you have an uninstall.php file so choose one method or the other!

Next Steps

This guide should help you understand how plugins are put together in WordPress.

Next, create a staging version of your website to really get into a plugin’s code. Extend it, break (and fix!) it… in fact, so long as you set up the staging version properly, you will be able to do anything to it!

Author Owen Radford

More posts by Owen Radford