A beginner’s guide to WordPress plugin development

WordPress is one of the most popular open source content management systems in the world, and it’s little wonder when you consider its user-friendly nature, quick installation process, and especially its adaptability. Ever since version 1.2 hit the market, WordPress has supported self-developed plugins. Taking this step allowed programmers to adapt the CMS to meet their individual needs, all without actually changing the source code. Since then, tens of thousands of WordPress plugins have been developed and released, which has allowed the software, originally developed for blogging, to make a smooth transition to managing websites.

The advantages of programming your own WordPress plugins

With the wide range of extensions on offer (most of which are available for free), and the extensive plugin directory found on the official WordPress website it’s not always necessary to develop your own WordPress plugins. But when searching for a suitable extension for your web project, you might find that many WordPress plugins match your search criteria, but one of the following issues occurs:

  • The plugin you want is no longer being developed
  • The plugin you want no longer functions the way it was designed to
  • There isn’t a plugin with the full range of functions that you need

These are among the most common reasons for creating your own WordPress plugin or modifying an existing one. Having your own extension can be an excellent alternative to conventional WordPress addons made with individual functions.php files. Copying prepared snippets of code in order to, for example, integrate Google Analytics into your web project is the quickest and easiest way to expand WordPress. When faced with larger projects, however, doing this often causes the functions.php file to become less clearly arranged, potentially leading to errors that affect the entire project. What’s more, users are also faced with the task of having to transfer any changes into the functions.php file whenever a new theme is adopted. Luckily, some plugins can help here by allowing you to individually manage all changes when switching WordPress themes. These plugins can be developed even if you have only limited programming experience.

Step 1: creating your WordPress plugin

WordPress is based on the script language PHP, which is why its plugins are essentially nothing more than PHP scripts. A simple extension can consist of a single PHP file with just a few lines of code, provided that this script adheres to its corresponding licence. WordPress is subject to the GNU General Public Licence (GPL), so make sure to select a licence that complies with these rules, such as GPLv2. In order to unify plugin codes, coding standards have been created to serve as programming guidelines, which comes in handy for those who wish to publish their plugins. To develop your own WordPress plugin, proceed as follows: 1. First, a PHP file needs to be created. Any word processing program can be used for this step (but when working with lines of code, it’s best to use a special program, like Notepad++). When saving the file, just select ‘All files’ and add the ending ‘.php’. For example, my.plugin.php. Copy and paste this file into the WordPress plugin directory—generally the file path, /wp-content/plugins/ is used here. For better structuring, especially with scripts that are composed of more than one file, it’s recommended to create a directory for your plugin: /wp-content/plugins/my-plugin. 2. In the file, you can add the following general information to your plugin:

<?php
/*
Plugin Name: PLUGIN NAME
Plugin URI: LINK TO PLUGIN
Description: PLUGIN DESCRIPTION
Version: 1.0
Author: YOUR NAME
Author URI: LINK TO YOUR WEBSITE
Licence: GPLv2
*/
?>

3. After including this code, you’ve essentially finished the initial step and have created your first WordPress plugin. You should be able to find it on your WordPress administration page within the list of plugins, including the description and two automatically generated links to your website and plugin page. You’ll also find buttons for activating and deactivating the plugin. These are, of course, still irrelevant at this stage of the development as no functions have been defined in your extension yet.

Step 2: breathing life into your own plugin

Once the PHP script’s header area has been created, you can start programming your WordPress plugin’s functions. To this end, you can use the snippet of code from step one that you’ve perhaps decided to include into the functions.php file. To illustrate this point, we’ve added some short code to our PHP file that will allow us to integrate an excerpt from Google Maps:

<?php
/*
Plugin Name: PLUGIN NAME
Plugin URI: LINK TO PLUGIN 
Description: PLUGIN DESCRIPTION
Version: 1.0
Author: YOUR NAME
Author URI: LINK TO YOUR WEBSITE
Licence: GPLv2
*/
/* Shortcode – Google Maps Integration */
function fn_googleMaps($atts, $content = null) {
    extract(shortcode_atts(array(
        "width" => 640,
        "height" => 480,
        "src" => ''
    ), $atts));
    return '<iframe width="' . $width . '" height="' . $height . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' . $src . '&output=embed"></iframe>';
}
add_shortcode("googlemap", "fn_googleMaps");
?>

Enter the corresponding embed code into the src attribute that you received from Google Maps, and presto: you’ve now developed your very first WordPress plugin. Now you’ll be able to find and activate it anytime under the tab ‘Maps’ in the navigation menu, and using its corresponding code, you can add it to your web project.

WordPress programming: practice makes perfect

The plugin covered in this Digital Guide article shows you don’t need to be a PHP expert in order to program your own WordPress extensions. But it’s important to remember that this example showcases a very simple implementation. For the most part, complex plugins that comprise multiple scripts require more than just a few minutes of work before a finished tool can be ready for use. More ambitious projects require a structured approach so as to keep errors to an absolute minimum. For this reason, it’s important to write the cleanest code possible from get-go, and don’t forget to incorporate informative comments that describe each function in detail. Ideally, the plugin should also feature a unique name that makes it clear to the user which tasks it supports. For further information on the topic, we recommend taking a look at the WordPress Plugin Handbook. From programming tips to suggestions for useful development tools, here you’ll find extensive support for both beginners and experts.

Was this article helpful?
Page top