How to code a successful WordPress plugin (Part 1)

petertill
3 min readNov 8, 2022

--

Nearly the 43.2% of the websites is made with WordPress. This very famous CMS has every feature you need to build anything from product landing pages to your personal blog. But the real power of it all lies in the plugins and its open source nature

That’s why plugins are so popular! In this article, I will teach you how to create one step by step! We will start by setting up our plugin source code directories. If your plugin becomes viral, you can even make it paid or sell it’s source code on https://codester.com for example!

Folder structure

First of all, every WordPress plugin should be located inside a specific directory. To find this directory you should open your web server’s root directory or the folder where you have wordpress installed. You can see a folder named wp-content there, and inside that there is a folder called plugins. Please note that every plugin should be at /wp-content/plugins/ !

Now, you can create a folder inside it with the name of your plugin. For this example, I will name it seowp as I gonna build an SEO optimalization plugin for wordpress. Inside your plugin folder, you should create these files! I will explain the files later.

Let’s protect our plugin from the malicious attackers!

In a cyber attack, hackers try to gather as much information as possible about the target, which in the case of WordPress websites is the WordPress version and plugins. With this simple trick, you can prevent your plugin from showing confidential information to hackers when they navigate to your folder via URL. Our index.php file will act as a placeholder file, so attackers cannot see the folder structure of our plugin and find vulnerabilities.

We’ll add a simple text to the placeholder file:

Now that we have made our plugin safe, we can move on to the next step!

Plugin metadata

Our init.php should contain the plugin’s metadata like the author, description, plugin name, version, licensing too. We need to provide this information in the form of a multi-line PHP comment like this:

Outputting a simple text in admin panel

The next thing we will do is to create a side navigation tab on the admin panel and create a dashboard for our plugin. WordPress uses hooks to communicate with plugins & we will use this feature because it makes the process much easier.

To create 2 side navigation tabs, we need to create those with a title, icon and slug! Append this code at the end of your init.php!

When activating you plugin (if you haven’t done this already), you should see 2 more tabs on the wp-admin page. If you click on them, you should only see a blank page.

So in this article, we have learned how to create the plugin’s directory tree and to add two more tabs into the admin panel & to secure our plugin.

Since you would read that whole tutorial in like 2 hours, I’ve divided this into multiple parts.

Thanks for reading, please consider to follow me on Medium so you will receive a notification about the next part!

Happy coding!

--

--

petertill
petertill

Written by petertill

I'm Peter Till, a student software engineer. I usually spend my time with creating projects in PHP, Java, C# and Python. I follow back everyone

No responses yet