Create your own plugin in WordPress

Auteur: 4BIS Innovations | Support

Gepubliceerd: 27.11.2020

We have already taken you into the wonderful world of WordPress plugins. This week I'm going to explain how you can make your own plugin based on a small example. I'm going to show you how to make a small plugin that adds bootstrap functionality to your website.

In previous articles we have talked about WordPress Plugins how easy they are and how much you can do with them. And while it may seem difficult to create a plugin, the beginning is actually quite simple. Only with the large plugins you need some knowledge of programming in PHP, but for a small plugin it is sufficient to have basic programming skills. Now that we have established that anyone ('who has the basic programming skills') can do it, the question now arises why should we do it?

Why a plugin?

Writing extra functionality for your website is very nice and very useful but why put it in a plugin when we can also put it in the functions.php file of the theme. To which I say: “yes, that's possible, but I'm lazy” Suppose we also need the functionality we just developed for website A for website B and C. Now we can of course copy the code from A to B and C, but if we had put it in a plugin it is simply a matter of activating the plugin on B and C.

Or a 2nd example suppose we have the functionality in website A and you or the customer for whom you have made the website wants a different theme because it fits your needs much better or has that specific piece of functionality that you have always missed . If your code had been in the theme, it would be gone now, but with a plugin you don't have to do anything.

Your first plugin!

Now that we've established that plugins are actually quite useful, I'm going to show you that the first few steps to creating a plugin are quite simple. Before you start creating a plugin, it is always useful to first consider why you are creating the plugin. As I said before I am a bit lazy at times and doing things for the sake of doing things is too much for me so before we start we need to know why we are starting. A small plugin I made a long time ago is the ability to add bootstrap to your WordPress site.

As a programmer, I'm not really into all the drag and drop builders that make the html and css for you, I find it much easier to just write the HTML directly with bootstrap classes. That is why I use bootstrap on almost all sites and I have written a mini plugin that adds bootstrap functionality to the WordPress site.

Step 1. Connection to your Server

The very first thing we need is a connection to the server where your WordPress Site is hosted. You can do this with an FTP program or if you are a programmer, this option is often already in your development environment. Make sure you connect via SFTP, this is the secure version. Once you are connected go to the folder with the plugins this folder is located in /wp-content/plugins/ by default.

Step 2. Create a new folder for your plugin

If you are in the plugins directory create a new name for your plugin. Be careful with your naming and make sure your naming is somewhat descriptive. Nobody knows the difference between plugin_1 and plugin_2 if you have to work on it again after a month. Once you have created your directory, we will create our plugin file in it. The common convention is that your file from which your plugin starts has the same name as your plugin directory. So we're going to create a file called fourbis-add-bootstrap-files.php

Step 3. Add the Plugin Information

Each plugin has a special block of code that WordPress reads and ensures that WordPress recognizes your plugin as a plugin.

Congratulations you just added everything to create a plugin. You can now activate your plugin in the admin area of your site. The only drawback is your plugin doesn't do anything yet so we now have to add some functionality so that our plugin actually does something.

Step 4. Functionality

Until here it was all easy and now the fun work starts developing functionality for our plugin. Our plugin is now like a blank book, we can still go in all directions! The easiest way to add functionality to your plugin is with actions and filters. Actions and filter are hooks that are in WordPress that we can hook into. If somewhere in WordPress the code do_action('manualAction') is executed we can hook into it with an add_action() method that takes 2 parameters the first is the action we want to hook into and the 2nd is the name of the function we want want to execute when WordPress executes that action.

What we want to do is add the bootstrap functionality to our website. This is done by adding a stylesheet and a javascript file. We can use the wp_enqueue_scripts action hook here with this hook you can add scripts and CSS stylesheets to your website.

Step 5. Merge Everything

The Last step is actually a small one and that is adding the functionality you just wrote to your plugin file. Once you've done that, your plugin is finished. Of course this is a very easy example that has already been chewed out but this was to show that the start of the plugin is not difficult at all, but the more functionality your plugin has to be able to do, the more complicated your plugin becomes. For example, the somewhat larger WordPress Plugins can quickly have hundreds of files.

Things to consider

The sample plugin is a very easy example and is a good way to get familiar with plugin development. If you want to experiment a bit yourself, there is a lot of information to be found on the WordPress Codex. After a lot of blood sweat and tears, if you have a well-functioning plugin, you probably also want an options page to configure things on. There are many other things that need to be taken into account during the development process.

Creating a WordPress plugin is a great way to learn the basics of how WordPress works. If you need help or are looking for experienced plugin builders for WordPress, please contact our development team.

Contact us

Niet kunnen vinden wat je zocht?_

Heb je het antwoord op een vraag niet kunnen vinden? Of heb je het idee dat er nog iets ontbreekt op onze kennisbank? Laat het ons weten. Stel je vraag direct per telefoon aan onze Supportdesk of zet je verzoek op de mail en je ontvangt binnen 24 uur een reactie.