Web Development wordpress

Create Your Own WordPress Plugin in 4 Easy Steps

WordPress is a highly extensible platform, with tons of plugins to address almost any task. Sometimes, however, installing a plugin can be an overkill. Instead, site owners can change a few lines of code themselves to achieve the purpose. If you want to know how to create a plugin with your own hands, this post is for you.

thumbnail

Often, WordPress websites can be customized without writing a single line of code. An abundance of publicly available free and paid plugins for various needs is what makes the WordPress platform so popular. Still, there are cases where a line or two of custom PHP code is needed. Advanced WordPress users can easily find those bits of code online and even add them to their websites. However, this is where all kinds of problems begin to emerge. 

This guide was created for advanced WordPress users who have basic understanding of PHP and, ideally, some experience with it. Do you want to improve the functionality of your website without breaking it? Then, read on!

How to Add Code Snippets to Your Website 

There are two ways to add something to your WordPress website: 

  • Install a WordPress plugin 
  • Add code directly to your theme’s functions.php file 

There is an ongoing debate regarding whether adding code directly to the functions.php file is better than using a plugin. Some people even argue that adding all changes to the functions.php file is better for performance. Spoiler alert: it’s not. 

Both approaches have their pros and cons, but today, we are going to focus on the first method. You’ll learn how to structure your code, organize your files, as well as activate, run and test your plugins. And remember – always create a backup of your website before installing anything. It will save you a lot of time if something goes wrong (and it almost always does)! 

So, What Is a WordPress Plugin?  

To put it simply, a plugin is a piece of software made from one or more files that “plugs into” your WordPress site to extend its functionality. Under the hood, it’s just lines of code written by someone else to provide new functionality or extend existing functionality on your website. From blogs and portfolios to online stores, portals and directories, we can create all kinds of websites using only one platform – WordPress. All of this is possible thanks to plugins.  

When You Might Need a Custom Plugin 

So, if there are so many plugins freely available online, why would anyone want to create their own plugin? A lot of website owners need to customize their websites even further. Ready-to-use solutions are awesome, but there is always a thing or two that needs to be changed.

And if you know how to do those things yourself – great! Now, you just need to know how to add your changes. A lot of WordPress users just add new code directly to the functions.php file and call it a day. While it might fix the problem in the short term, creating a separate plugin is almost always better. And here is why. 

Your functions.php will be overwritten with the next theme update, so your changes will definitely be lost at some point in the future. Some website owners use third-party plugins to add code snippets, but if you are looking for a more lightweight and customizable solution, consider creating a custom WordPress plugin instead. It’s way easier than you think. In fact, it can be done in only 4 steps!

The Anatomy of a Custom Plugin 

Did you know that you can see the underlying code of all plugins installed on your website? Just go to Plugins > Plugin Editor and use the dropdown on the right to switch between plugins. Below the editor, you can find the documentation and, on the right, access all plugin folders and files. Do not edit those files! 

While you should be extremely careful while browsing those files directly, it’s a great way to see for yourself how your favorite plugins are structured. You’ll quickly notice that experienced plugin developers split their code into multiple files, leave plenty of comments, etc. In general, here are the main parts of any custom plugin: 

  • The main plugin file (this is the most important file out of all of them) 
  • The Main folder (where all of these files live) 
  • Folders for different file types 
  • Scripts (JavaScript files) 
  • Stylesheets (CSS files) 
  • A README file that contains information about the other files in a directory 

Pro tip: If you plan to create a really small plugin that contains a few chunks of PHP code, you will only need the main plugin folder with the main file inside. 

Create Your Plugin 

Plugins solve all kinds of problems, but they can create problems too. That’s why it’s important to know and follow the best practices in plugin development. 

1. Create the Main File and Folder

Every plugin needs a separate folder with the main file inside. It will always be a PHP file with commented-out text that contains important information about your plugin.  

<?php 
/*Plugin Name: <NAME> 
Plugin URI:  <http://LINK TO THE PLUGIN HOMEPAGE> 
Description: <THE PURPOSE OF THIS PLUGIN> 
Version:     <VERSION> 
Author:      <YOUR NAME> 
Author URI:  <YOUR WEBSITE > 
License:     <MOST WORDPRESS PLUGINS ARE RELEASED UNDER THE GPL> 
License URI: <https://LINK TO YOUR PLUGIN LICENSE>*/ 
/*Your code snippet here*/ 
?> 

WordPress uses this information to populate the plugins screen on your website: 

Out of all of these key-value pairs, only the plugin name is required. And if you are not planning to distribute this plugin, you don’t really need all of this information. However, we would also recommend adding a plugin description, so you can easily tell what this plugin does without even opening it (and your future self will thank you).

Other information about the plugin can be found in the README.txt file that populates the plugin’s page in the plugin directory. Again, you don’t have to add it if you are just creating a small WordPress plugin for your needs. 

Now, let’s create a new folder with an empty file inside. Copy the code snippet above and add it to the new file. Give them both a meaningful name and include a prefix (.php) after the file’s name. Once you have a folder with the main file ready, move it to the wp-content/plugins directory on your website.

Now, when you go to Plugins > Plugin Editor, you should be able to see your plugin in a dropdown menu on the right. The plugin will be also visible on the main Plugins page. But it doesn’t do anything yet because there is no actual code inside. 

2. Add PHP Code

It’s a good practice to split your code into multiple files. However, it depends on what you are trying to achieve. If you are just adding a few lines of PHP code, you can add it to the main file directly. You’ll see that we saved some space in our template for code snippets. 

Now is a good time to add your functions to this file. You can use a plugin editor to do it or, better yet, edit the file on your computer using code editors (Visual Studio Code, Codespaces, Atom, etc.) or simple text editing software. Then, just move your files to the website when they are ready. For more complex plugins, consider creating additional PHP files and storing them in a separate folder.

3. Add Custom CSS and JS

Create stylesheets and scripts as separate files and enqueue them. To do that, we will use wp_enqueue_scripts to add JavaScript files and wp_enqueue_style to add CSS files. The add_action hook is used for enqueuing both stylesheets and scripts.    

function myfunction() {     
wp_enqueue_style( $handle, $src, $deps, $ver, $media);}add_action( 'wp_enqueue_scripts', ‘myfunction' ); 
}

Pro tip: Instead of creating everything from scratch, consider using free and ready-to-use WordPress plugin templates that can be found on GitHub. 

4. Activate, Run, and Test Your Plugin 

And finally, it’s time to run your custom plugin! Navigate to the Plugins view, find your plugin, and click on Activate. Ideally, you would do it on a staging website to make sure everything works as expected. Don’t forget to test it thoroughly.

Even if you are 100% sure that everything is correct, some functions might be deprecated or incompatible with your current WordPress or PHP version. To see if there are no errors or warnings, check your PHP error log or use the Error Log Monitor plugin to see the latest messages from your PHP error log right on your Dashboard. 


Top-Class WordPress Development Services from PSD2HTML

While building a simple WordPress plugin is a task that you can handle yourself, creating plugins to address serious business issues is something that requires professional expertise. That’s exactly what our WordPress development team can offer you. With 16+ years of industry experience and thousands of successfully completed WP projects, we know everything about the world’s most popular CMS.

Reach out to us with any WordPress-related request, from building engaging custom themes or modifying existing ones to performance and security optimization.

Helping your business grow is our number one priority!

Valerie Muradian

Valerie is a top writer, software developer, and book lover. She writes on the latest technologies, self-development, life-long learning, creativity, and everything in between. | Follow her on Twitter | Read her on Medium