Develop WordPress Plugins: How To Program Your Own Extensions

Develop WordPress Plugin

The fact that WordPress is so popular is not only due to the fact that the content management system is open source, quickly installed and easy to use. The resounding success of web software is mainly due to its modifiability. Already since version 1.2, which was released just four months after the first stable WordPress version, WordPress also supports plug-ins developed in-house. This enabled programmers to adapt the CMS to their own needs at this early stage – without having to modify the actual source code. Since then, tens of thousands of WordPress plug-ins have been programmed and published, allowing the former blog software to be flexibly adapted for the design and administration of websites.

The advantages of having your own WordPress plug-in

In view of the numerous extensions, which like WordPress itself are mostly free of charge and available for download in the plug-in directory of the official website, it does not really seem necessary to create your own WordPress plug-in. However, when searching for the right extensions for your web project, you will soon find out that many plug-ins (here we have presented the best WordPress plug-ins for you) may match your search criteria, but either …

  1. no longer be further developed,
  2. no longer function or do not function as desired
  3. or do not have the required functional scope.

It can, therefore, make sense to get involved with WordPress programming and create your own plug-in or redesign an existing one for your own purposes. An own extension is also an excellent alternative to the common functional extension of the WordPress installation by means of changes in the individual functions.php of the used theme. 

Copying finished code snippets, for example, to integrate Google Analytics, is the fastest and easiest way to extend WordPress, but in complex projects, it often leads to the functions.php becoming unclear and errors affecting the entire project. In addition, you have to transfer all the changes you have made laboriously when changing the theme. 

Own plug-ins, with which you can manage all adjustments individually and comfortably, on the other hand, can easily be transferred when you change your WordPress theme. Your development can also be mastered even with little programming knowledge.

Step 1: Create WordPress plug-in

WordPress is based on the scripting language PHP, which is why WordPress plug-ins are basically nothing else than PHP scripts. A simple extension can consist of a single PHP file with only a few lines of code, where you are only restricted in programming in terms of licensing. Since WordPress itself is subject to the GNU General Public License (GPL), you should choose a license that is compatible with it, such as GPLv2. In order to unify the code of the plug-ins, coding standards have also been formulated, which are not mandatory, but make sense if you want to publish your plug-in. If you are developing your first own WordPress plug-in, it is best to proceed as follows:

  1. First of all, a PHP file must be created. You can use any text program such as the standard editor (at the latest, however, if you are working with lines of code, a special program such as Notepad++ is recommended). When saving, simply select “All files” and append the extension .php, for example, my-plug-in.php. Now simply copy this file into the plug-in directory of your WordPress installation – by default /wp-content/plugins/. For better structuring, especially with scripts consisting of several files, it is recommended to create a subdirectory for your plug-in: /wp-content/plugins/my-plug-in.
  1. You can now insert the first few lines with general information about your plug-in into this file:

<?php

/*

Plugin Name: NAME OF THE PLUG-IN

Plugin URI: LINK TO PLUG-IN

Description: DESCRIPTION of the PLUG-IN

version: 1.0

Author: YOUR NAME

Author URI: LINK TO YOUR HOMEPAGE

License: GPLv2

*/

?>

  1. This means that the first step is basically already completed and you have created your first WordPress plug-in. You will already find it on your WordPress administration page in the list of plug-ins, including the description and the two automatically generated links to your homepage and the plug-in page. You will also see the button to activate or deactivate it, but of course this is completely irrelevant at this point, since no functions have been defined in your extension yet.

Step 2: Bringing your own plug-in to life

Once the header of the PHP script is created, you can start programming the functions of your WordPress plug-in. For this purpose, you can, for example, use the code snippets already mentioned, which you may have inserted into functions.php so far. As an example, we have added a short code to our PHP file at this point, which can be used to integrate a Google Maps snippet:

<?php

/*

Plugin Name: NAME OF THE PLUG-IN

Plugin URI: LINK TO PLUG-IN

Description: DESCRIPTION of the PLUG-IN

version: 1.0

Author: YOUR NAME

Author URI: LINK TO YOUR HOMEPAGE

License: GPLv2

*/

/* Shortcode – Google Maps integration */

function fn_googleMaps($atts, $content = zero) {

   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 . ‘&amp;output=embed”></iframe>’;

}

add_shortcode(“googlemap”, “fn_googleMaps”);

?>

If you now enter the respective embed code that you get from Google Maps (“split or embed map”) into the src-attribute, you have successfully developed your first own WordPress plug-in. You can now find and activate it at any time in the navigation menu under the tab “Maps” and insert it into your web project with the corresponding code.

WordPress programming: Practice makes perfect

The example plug-in presented in this guide shows that you don’t have to be a PHP crack to program your own WordPress extension. However, the example is also a very simple implementation, while complex plug-ins consisting of several scripts usually cannot be written within minutes. 

For more complex plug-ins, you have to proceed in a very structured way, so that sources of error ideally do not occur in the first place or at least can be quickly discovered and corrected. Therefore, make sure to write clean code from the beginning and include meaningful comments that explain the respective function in the best possible way. The name of the plug-in should also be self-explanatory at best and never be identical to an existing one.

For more information about “Programming a WordPress Plug-in,” we recommend having a look at the English speaking developer forum of the web software. In the WordPress Plugin Developer Handbook, you will find detailed help for beginners and advanced users – from programming tips to recommendations of useful development tools.

Leave a Comment