first plugin on wordpress, general impression, some nuances
So this is a subjective article on the heels of how to write your first plugin for the engine of some site. And in this case we will talk about WordPress.
This article is for beginners. In our case, we consider ourselves amateurs in web development.
When you are already a pro in some field, you are annoyed by amateurs and their articles on how to write something for the first time, and this is understandable. But in fact, the first experience is always more interesting for those who do not understand the topic at all. Therefore, our article with a simple level is for you, dear noobs.
What was the reason?
So why did we decide to write our own plugin? The main reason is that we did not find the functionality we needed among the existing plugins. As everyone knows…
For example, we have a certain API for controlling certain equipment via Ajax requests.
That is, our idea is to give the user of some cloud program, who works in this program from a browser, the ability to control equipment locally connected to his computer.
It seems like there's nothing complicated about it?
It's really all trivial – write a server (or service), for example, under Windows, which will listen to some port on localhost, accept commands via http protocol, execute them and return the result back to the client. Well, you still just need to bypass the browser's CORS protection, but that's not difficult.
What is the benefit?
As always, nuances decide everything. The fact is that, according to professionals, any program installed on a user's computer breaks down when a certain threshold of value is reached. I will not argue with this statement, but let's assume that this is so.
Then the conclusion remains that it is necessary to somehow NOT install the program on the user's computer, but what to do?… The correct way is to output the program to the cloud, namely to your server.
And from my own experience I can say that this process is in full swing. But in the overwhelming majority of cases, cloud program developers make their services to support equipment only for their specific cloud program. And this is logical, because their goal is to make money on their cloud program.
Look around
What is missing cloudy programs? There are practically no free drivers for controlling equipment locally connected to the user's computer.
And it's true, for example, in WordPress I personally have never encountered this. For example, only in webasyst I came across a plugin that exploits kkmserver.
Perspective
Now let's assume that your driver does NOT support one specific model of equipment from one manufacturer, but all models from different manufacturers (of course, for the same type of equipment) according to a uniform protocol. And then you provide an obvious benefit to the users of your cloud program, because everything that your plugin supports is supported by your cloud program. The list of equipment is easily expanded by plugin developers in the future, etc., etc.
And perhaps you are also shifting the solution of technical problems with the equipment onto the shoulders of the plugin developers. And the plugin developers may not be against it, since the plugin is free, but to control the equipment, you need a second part – a service program installed on a PC, which is unlikely to be free. In any case, the user decides, if it is convenient for him, then he will be ready to pay a certain amount.
Unified protocol
Equipment manufacturers (competitors) and manufacturers of ready-made software are also competitors and they will never be able to agree on a single protocol for equipment control. But programmers like us in our niche can make a single protocol and convert it to the native protocols of each manufacturer on the user's OS side. That is, there is another program that is installed on the user's computer (server, service, call it what you want).
When and who will need our plugin?
Here lies the first and probably the main nuance. On the site you can be a seller (for example, you are an admin) and you must have buyers, of course, which is logical if you are engaged in commercial activities.
If you need the seller to manage his equipment, then the plugin should work in the seller's interface.
If you need the buyer to control some of his equipment, then the equipment should work in the buyer's interface. However, I personally find it difficult to imagine such equipment, except perhaps IoT (for a smart home).
So here is our plugin for the seller's interface, which should, for example, punch a check and accept a bank card from the buyer when picking up the goods from the office. That is, when the buyer has chosen to pay upon picking up.
What's the best way to start developing your own plugin?
To write your own plugin, it’s probably better to look for one with similar functionality.
For WordPress this (in our case) turned out to be Woocommerce Manual Payment.
Next, it is better to first study (with a PHP debugger) how its code works and at some point throw out all the unnecessary stuff and add your own functionality. But this is easy to say. In practice, when you still don’t know at all how the WordPress engine works, it is difficult to determine which methods and classes are needed and which are not.
And here I want to say that I understood why WordPress is a world leader, well, or one of the leaders. The reason is the correct logic of the code, the division of functionality into classes, classes into directories and, of course, the openness of the source code.
Then the process of unwinding someone else's code goes calmly without euphoria or nervous breakdowns (well, that's in our opinion). We throw out the functionality of someone else that we don't need in pieces and at some point we are left with essentially one plugin template.
And we must admit that we did not read any tutorials on how to create our own plugin. Let's not deceive ourselves – the best tutorial is the developer's source code. Of course, we watched a few videos on the Internet, but everything turned out to be very superficial. In fact, there are too many videos, maybe this is the problem.
In general, our advice is to study the code using the example of some other plugins and study the code of WordPress itself. This works 100%.
Xammp
And of course, install WordPress locally on your computer, for example, using xammp or something similar. Then you will be able to debug php code, for example in the environment VS Code.
Use global search
It is probably worth saying that when studying the work of the code, most often you will have to look for some method or variable name and do it better, for example, in the editor notepad3++.
The tool decides everything
We search globally in all WordPress files for the use of some entity, then set a breakpoint in the found section of code with the debugger and wait to see if it works or not.
If it worked, we can go further through the code or go back, looking at the stack of called functions. And the debugger, for example XDebug shows the contents of variables, whether they are objects, arrays, etc. This is very convenient.
What you need to be prepared for in WordPress
How to connect your js script files. There is a useful method for this in php.
How your styles, css files are connected. There is a method for this in php.
How to use jquery dialog forms
In our case, it turned out that we just needed to include this script in WordPress, that is, it is already in the engine.
wp_enqueue_script('jquery-ui-dialog');
Note: you can check which js or css files are already connected on the page simply by looking at the source code of the page result. We look for the same lines where there is xxxxxxx.js . The point is that all external resources are still connected as a result in a standard uniform manner and this is visible in the source code of the page result (Ctrl-U).
admin-ajax.php
Why does your page constantly reload periodically?
It turned out that this is one of the most important features of WordPress. It is a mechanism for exchanging commands with the site, that is, you are the client (this is your browser page) and there is a server where all the information is stored. The client exchanges commands with the server, for example, to receive order data from the server or to change the order parameter on the server.
In fact, this is a regular Ajax request, in the transmitted parameters of which WordPress has reserved a certain behavior (a certain functionality, interface).
WordPress itself uses this mechanism, for example, to periodically update a page, for example, it is relevant for multi-user modes, that is, when several users work with one order and changes made by one user become visible to another after some time (several seconds).
But you can also send your requests to the server via admin-ajax.php with the purpose of, for example, saving an order with a new status on the server.
Results
In general, it takes about one working week to develop the first plugin for WordPress, which in our opinion is a very good result, especially considering that we have not written plugins for website engines yet. But the most important thing is that the feeling of literacy and convenience of the WordPress engine code remains. In general, respect to WordPress. Next, we will look at Joomla or Webasyst.
If anyone is interested, the plugin is posted on GitHub https://github.com/PavelDorofeev/API-receipt-fiscalization-for-CMS-and-CRM .
The plugin is free, under the GPLv2 license, as required by WordPress. But generally speaking, the plugin is part of a commercial project, the name of which I will not disclose, so that I do not get banned again and I will not post the video here for the same reason. If anyone is interested, write to me in private.