Online Software

How can I create a plugin for WordPress?

create a plugin for WordPress
Code acclaim: your plugin could be seen by millions

Currently powering almost a third of the internet, WordPress has become insanely popular due to its open-source nature and the vast array of plugins and themes available  to extend your website further. At the time of writing, there are more than 55,000 free plugins available from WordPress.org.

Assuming you have a basic knowledge of PHP, creating your own plugin for WordPress is actually quite easy.

Create a plugin for WordPress: what you’ll need

Requirements for this are quite simple:

  • An IDE, code editor or even just a simple notepad app to write code in
  • A WordPress site to test your plugin on (preferably not a ‘live’ site, as there’s a good chance you may break it)
  • If you plan to make it available, for free, from WordPress.org then you’ll need basic knowledge of using SVN. We’ll come to this later

To GPL or not to GPL

WordPress is published under a GPL licence, which means anybody is free to copy, modify or even sell it (with appropriate attribution). That extends to plugins that are hosted at WordPress.org. Your plugin must therefore be compatible with the GPLv2, or any later version.

If you wish to sell your plugin you will need to host it yourself – the downside to this is that WordPress looks at the WordPress.org directory for plugin updates so, by default, your plugin will not automatically update for users (you can code something to change this, which is what many premium plugins do).

What’s the advantage of using WordPress.org? Well, you get added to a vast directory, which is the number one place that people turn to for plugins. They include free hosting, analytics and a support forum as well. For the purposes of this article, I’m going to assume you’re going to be using WordPress.org.

What plugin do I write?

Well, that’s the $64,000 question, isn’t it? I started writing plugins when I needed to do something on my site that an existing plugin didn’t do. Of course, you may be in it for the fame and fortune.

I’d steer away from recreating something that already exists – for example, if you write a plugin that adds a snow-falling effect to a site, it’s likely to be rejected as there are so many of these already.

You may also want to consider adopting an abandoned plugin – the WordPress.org directory is full of plugins that, although they may work (or may not require much work to get them to do so), have been abandoned by their developers. You can identify these as they will have a banner over the top indicating that they’ve not been updated for more than two years – look too at the plugin’s support forum to see if the developer is answering there too.

If not, chances are it’s been left to rot. Do what you can to contact the original author and, if that fails, contact the WordPress.org plugin team and, if they agree, they’ll hand it over to you. They’ll probably want to see some existing plugins of yours first, though, to make sure you’re up to the task.

Alternatively, remember that GPL licence we discussed before? You can fork your own version of any plugin from the directory, assuming you’re going to do something different with it.

The basics

A WordPress plugin requires two things to identify it – a README file and header data in the main plugin file. The former is only really required if you’re planning to submit it to WordPress.org.

The README

The README (or rather readme.txt) is used to provide essential information to WordPress.org users. It’s written in markdown format and WordPress.org provides an example of a standard layout – simply download and modify this to taste (and remove anything that’s irrelevant). Once written, you can use the README validator to check that it’s well formed and that you’ve not missed anything essential. The readme.txt is placed in the same folder as your main plugin file.

The plugin header

This is where you get to create your plugin file. Let’s call it plugin-example.php. Open the file and add the following to the top:

In fact, if you zipped this file up right now and uploaded it to your site as a plugin, it would display on your list of plugins and you would be able to activate it. Albeit, it doesn’t do anything.

Sorry, how do I upload my own plugin?

  • Sign into the administrator page of your site and click on Plugins -> Add New
  • At the top is a little button named ‘Add New’. Clicking on this will let you upload a zip file
  • Upload your zipped plugin and activate

How do I write a plugin?

With a little knowledge of PHP, writing a plugin isn’t particularly difficult. Each WordPress installation comes with a plugin named ‘Hello Dolly’, which is included as a coding example. Have a look at that to grasp some of the basics. The great thing about WordPress is that it has hooks (in the form of actions and filters) for most things, allowing you to add to or modify existing content.

The plugin handbook is a great place to start, which includes layouts for all of the available hooks as well as general advice on plugin development.

Here are some examples of code of my own to give you further ideas:

This code will display a message above any posts that were published before 1st January 2013. This is a good example of using a filter named the_content to modify the output of a post.

The next introduces shortcodes, a code that can be added by a post author to add additional information to the content:

In this case, anybody can add [wikilink] and [/wikilink] around some text to automatically link it to the relevant Wikipedia page. At line 5 we look for additional parameters (you can read more about its use here).

Now we get a bit more complex:

This will add a “time to read” to the top of your posts. But unless the post has been updated, this is going to be the same each time the post is displayed, so I’m using transients to cache the output.

Hopefully, these three pieces of code should give you a start on some basic WordPress concepts  – you can find more WordPress code snippets on my Gist, or download and dissect some of my own plugins. You may notice here that I’m hosting my plugins on Github – this is a great place for development. I then export any final releases to the WordPress.org SVN. So, let’s discuss this next…

Hold on, how do I get my plugin on WordPress.org?

You’ve written your plugin, thoroughly tested it on your own test site and you’re ready for the world to see the end result. Let’s get it on WordPress.org.

  • Read the plugin guidelines to make sure you’re not doing something you shouldn’t
  • Make sure your code is up to scratch – the WordPress coding standards are a good place to start. If your plugin is insecure, for example, that’s a quick way for it to be rejected
  • Upload your plugin to WordPress.org
  • It will now be manually reviewed, which takes up to ten days. They attempt to review all plugins within five business days of submission, but the process takes as long as it takes, depending on the complexity of your plugin (it is run by volunteers, so please bear this in mind before hassling them about how long it’s taking)
  • Meantime, you will have a URL allocated to you for your plugin in the format https://wordpress.org/plugins/[your plugin name]
  • Once approved, you will receive an email with your SVN details. You will need to upload your plugin (and subsequent updates) to this repository. Needless to say, there is documentation on using SVN, if you’re stuck.

Maintaining your plugin

Once it’s live, you can promote your plugin and, fingers crossed, it will be well loved. I’d recommend you do the following to keep your plugin well oiled…

  • Keep it up-to-date. Even if you’re not interested in adding new features, make sure it works with the latest version of WordPress. If no changes are required, you should update the “Tested up to” tag in your README file to reflect that it works fine – otherwise WordPress will flag it as not compatible and you may lose users.
  • Follow and and answer support queries. Each plugin has its own forum – subscribe to it and answer any queries. Users will appreciate it and you’ll get valuable feedback on your plugin. Make sure you read the forum guidelines, though – using signatures, supporting premium versions of your plugin and asking users for site access are all things that will get you into trouble.
  • Love your reviews. Users can review your plugin and, yes, you will get people hating it. They may install it, get an odd message that, as it turns out, is nothing to do with your plugin and then give you a one-star rating. Admins will not remove these and, in my experience, how you deal with these can be more important to how other people see you than the actual review. Here’s an example where an argument about a review has ensued, causing the admins to step in. In comparison, here’s a review from one my own plugins where I don’t argue the case but, instead, try and help them all I can.

This article is written by David Artiss, an employee of Automattic, the company behind WordPress.com. He writes here in a personal capacity and the article is not endored nor sponsored by his employer.  

About the author

David Artiss

With 28 years of experience working in the IT industry, David has been writing about technology on his own personal site for many years. He is a keen gamer and happily admits to being a gadget nerd too.

Add Comment

Click here to post a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: