Getting Started With WordPress Plugin Development

0

WordPress is a great system as it is, but what really takes it to the next level are the plugins that extend and improve its functionality. Creating your own plugin will expand your knowledge of how WordPress works and, if it fills an unmet need, it could earn you some money too!

This tutorial is going to assume some existing knowledge of WordPress and PHP. If you need a helping hand starting with PHP, I recommend looking at the W3Schools PHP Tutorials.

It Begins With a Simple File

All you need to do to start is put a simple PHP file in the folder, modules. That’s it. As things get more complex you’ll want to make a subdirectory for your plugin, but for now – and for one file plugins – you can simply create a file in modules.

Name it according to what it does, but bear in mind you need to be specific. People often trial multiple plugins that do the same thing and your name can’t clash with somebody else’s. So seo.php is probably out, but owens-superb-seo-plugin.php is probably fine. Do a quick search to check for clashes.

This base file has to start with a specially formatted comment so that WordPress knows this is a plugin. It’ll need to look something like this:

<?php
/*
Plugin Name: Your Plugin's Name Here
Plugin URI: http://www.urlforyourplugin.com/
Version: Current Version Number
Author: Your Name Here
Description: What does the plugin do?
*/
?>

You now have a plugin. Right now though, it doesn’t do anything, so let’s move on!

Putting Your Plugin Together

Plugin Hooks

In order to ensure functions are carried out at the appropriate time, you’ll want to use a WordPress Hook. These hooks look out for any functions that need to run and run them at the appropriate time. So in our SEO plugin example, our hooks could be looking out for a page loading and might overwrite some of the default meta data with the user’s new settings.

Hooks come in two types – Actions and Filters. A filter receives a value and, depending on the code, may modify it before returning it. An action will execute your code at a certain point. Broadly speaking, an action allows you to do something and a filter changes data.

To see which hooks are available, check out Adam Brown’s WordPress Hooks Database.

Modifying the Database

All but the most basic plugins will need to manipulate the database. WordPress offers a few different methods for doing this:

The Option Mechanism

These allow you to save named pieces of data into the database as strings, arrays or PHP objects. Use unique names and try to minimise how many you use – remember you can use an array. This will probably be data related to the setup of the plugin and isn’t expected to regularly change.

To create a new option use the form:

add_option($name, $value)

To retrieve the option:

get_option($option)

To update an option:

update_option($name, $value)

Custom Taxonomies

These allow you to classify objects into groups. For example, if different types of post will receive different actions, these could be part of a custom taxonomy.

Post Meta (Custom Fields)

You can use this method for changing the data relating to specific pages and posts. Here’s an example.

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

You could also create a custom database table – it’s a little advanced for now, but it can be a very flexible method for taking your plugin building to the next level.

Activate!

This is all well and good, but without being able to activate it the plugin will be pretty useless!

We’re going to use those handy hooks again to take care of activating and deactivating your plugin:


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

I’m sure your users would never want to do this(!), but just in case it happens we should add a method to allow this and clear up after your plugin.

There are two ways to go about this:

  1. Put all the necessary code into a file name uninstall.php.
  2. Use a hook within the plugin – a function containing the code to be run once on uninstallation.

If you go down the uninstall.php route, WordPress will automatically run this file. You should be aware that no uninstall hooks in your plugin will run if you have an uninstall.php file. Use one method or the other!

Final Words

I hope that’s helped you understand how plugins are put together in WordPress. As all the code in a plugin is available, I’d recommend creating a staging version of your website and getting your hands dirty in a simple plugin’s code – manipulate it, break it, fix it and extend it knowing that (if you’ve properly setup your staging version) it doesn’t really matter what you do to the code.

Further Reading

For a deeper dive into the WordPress API, coding standards, PHP and more, visit the following links.


This post was written by Owen Radford. We are very grateful that Owen has written this post for us, however, the views expressed here belong to the author, and do not necessarily reflect the views and opinions of wpContent.
Share.

About Author

Owen works for Elementary Digital, a digital agency in London and Leeds that specialises in WordPress and Magento development.

Leave A Reply