Hello, ladies and gentlemen! In this article, I will explain how to create your own plugin for WordPress and why you need it. This is a very simple task that even a beginner can handle. By the end of this article, you will be able to create plugins on your own.
I also include a video tutorial that visually demonstrates the process.
Why create your own plugin for WP
So, you might ask, why create your own plugin for WordPress? After all, there’s the functions.php file where custom code works just fine. Partly, yes, but it’s not that simple. Let’s consider a hypothetical situation.
I have custom code for script optimization, other codes connected for speeding up WP, as well as counters and analytics tools on the site. And I decided to choose a suitable theme for the necessary set of tools, while also testing the speed:
- Option 1: With each theme change, I transfer my work from functions.php to a similar file in the new theme. It’s not difficult, but it’s extra work.
- Option 2: I create a custom plugin, which is no harder to make than a child theme.
- Option 3: For “geniuses”. I add the necessary functionality at the core level. Never do this!
You understand that the second option is the most convenient. You can create your own plugin in just a few minutes.
Now, let’s get to it. Do the following:
- Add all code related to the theme’s functionality to functions.php.
- Put all code that is not related to the theme’s functionality into a separate plugin.
These are not just good practice rules but an approach that simplifies life, for example, when changing themes.
Of course, some try to avoid connecting extra plugins, as there is a myth that the more plugins, the slower the site. And that’s nonsense. It doesn’t matter where you put the code, whether in functions.php or in a custom plugin, it will consume the same amount of resources. So, feel free to make your own plugin, it won’t slow down your site.
In terms of connection priorities, you’ll get roughly the same result.
So, if the code is not directly related to the theme’s operation, for example, if you add asynchronous loading of scripts that can work with any theme, analytics tools, or various functionalities that don’t affect the theme, add all this to the plugin.
But, for example, if you manually set async tags for the resources of your site’s current theme, add them to functions.php because resources in another theme may differ from the current one. The same applies if you, for example, reassign the theme template hierarchy. This should all be done within the child theme.
Creating a WordPress Plugin
Now, let’s create a plugin ourselves. All plugins are located in the folder: your-site/wp-content/plugins/.
You can place a separate PHP file in this folder as a plugin. Let’s create a new file with the .php extension.
You can place your PHP file directly in the plugins folder, or you can create a separate directory. I preferred to create a directory.
Come up with a unique name, it’s better to check in advance if there are any plugins with the same name in the repository.
Now you need to open the previously created PHP file and add the commented code:
<?php
/*
Plugin Name: Название плагина
Plugin URI: http://страница_с_описанием_плагина_и_его_обновлений
Description: Краткое описание плагина.
Version: Номер версии плагина, например: 1.0
Author: Имя автора плагина
Author URI: http://страница_автора_плагина
*/
?>
For example, mine looks like this:
<?php
/*
Plugin Name: Ren Plugin Custom
Plugin URI: http://decore3000.ru
Description: Плагин с моими кастомными функциями
Version: 1.0
Author: Я
Author URI: http://decore3000.ru
*/
?>
What do we have here?
- Plugin Name. The name of the plugin. You can use Cyrillic and other languages for the name. It doesn’t have to be a unique name, but if you plan to add it to the repository in the future, it’s better to come up with something different from other plugins. This is the only required parameter; everything else is optional.
- Plugin URI. The website address of the plugin. If there is a resource with documentation, a description of the functionality, and examples of the plugin’s work, specify it here.
- Description. A description of the plugin. If you plan to add it to the repository, make it clear and detailed. If not, you can omit this line.
- Version. Needed for development. Suppose the initial version is 1.0. Add new code, update the version to 1.01, and log what changes were made in this version. If you plan to use GIT, be sure to use version control.
- Autor. Specify the author’s name, you can proudly write “Me”.
- Author URI. The website address of the plugin’s author.
That’s it, essentially the plugin is ready. You can already activate it, but why have an empty plugin? Let’s add some code to it.
Noticed an error? Great, let’s keep the error and try to activate the plugin.
As you can see, I received a fatal error, so the plugin was not activated. This is another advantage of your own plugin over functions.php. If I make a mistake in the theme file, I will break the entire site, but if I make a mistake in the plugin during development, I simply won’t be able to activate it.
But keep in mind, if you mess up in an already activated plugin, the site will still be broken. It’s better to create a test site and plugin for checking new functionality.
We fixed the error, clicked the “Activate” button again. Great, the plugin works, and through it, I added redirects and Yandex Metrica. Now they are not tied to the theme.
And the code in the plugin looks like this:
In principle, we have completed the task. As you can see, creating your own plugin for WordPress is very simple, a beginner can handle it without any problems. Of course, I didn’t add complex functionality or create a graphical interface. But that’s a separate topic that requires a deeper understanding of WordPress.
Of course, developing new functionality can cause you problems, but don’t worry, most functions were developed long before you, and you can find them freely available, including on my website or Stack Overflow.
Your own plugin is ready
As you can see, the task turned out to be easy. Yes, creating your own plugin for WP will definitely not cause you any problems now. The problem is developing functionality. So go ahead, I already explained how to solve the simplest problem.
All the best, I wish you success and working code!