10 basic skills of the WordPress plugin developer

 developer wordpress

The plugins are great part of the greatness and beauty of WordPress and create your own plugin will give you a knowledge and freedom that you will not find if you simply use what others create.

Now, if you throw yourself into the WordPress plugin development it does not hurt to keep this in mind ] 10 basic tips for creating plugins :

:: 1. Creating the plugin ::

The first step is to create the folder of your plugin in the directory / wp-content / plugins . Once you have created the folder you must place the files of your plugin in that folder. There must be a main plugin file, and the file names must have only letters and hyphens (-) if you want to separate words, do not use other punctuation marks or spaces.

A type name would be something like this: plugin-seo-sencillo.php

The main file of the plugin must have the following structure for WordPress to identify it as a plugin:

Once you save the changes , even if you do not have the plugin yet, you will see it on the WordPress plugins page.

:: 2. Activation and deactivation of the plugin ::

Plugins can be activated by clicking on the link " ] Activate "from the list of plugins. In a basic plugin you do not have to do anything when activating it. But an advanced plugin will require tasks such as launch the plugin settings, create tables, etc., so let's see how we can manage the activation and deactivation of a plugin.

Plugin activation hook

WordPress offers a function called register_activation_hook that is launched when a plugin is activated. We can add custom functions that are executed when activating the plugin using this method. An example would be the following:

You have to define the path of the file that contains the activation function as the first parameter and the name of the function as the second parameter.

If the activation function is inside the main plugin file you can use _FILE_ as in the previous example. Within the activation function you can also perform tasks such as validations, initializations and creation of tables

Plugin deactivation hook

We can manage the deactivation of a plugin with register_deactivation_hook using a similar syntax to that of activation. In the deactivation function you can also perform additional actions such as cleaning plugin resources, settings and tables.

:: 3. Creation of custom tables ::

The structure of the WordPress database and it is very flexible, and you can implement most of the custom features using the tables that are available.

However, sometimes you may prefer to include more advanced systems, such as shopping carts, task management systems , subscriptions, etc.

In these cases you should know how and when create custom tables . The first thing is to take into account the requirements of your project and try to use the table and meta tables wp_options to store the data specific to your project. If you consider that these tables do not have the necessary structure to implement the functionality you need then create custom tables using the following method:

First we check if the table exists before creating it. You can decide to create the table at the time of activation, depending on what you need. In the example you will see that {$ wpdb-> prefix} is used before creating the name of the table. This is because, normally, WordPress tables start with the prefix wp_ . This can be changed when you install WordPress, so it's better not to manually add the prefix wp_ since it could be another, and so we make sure it will adapt to what exists.

Using {$ wpdb -> prefix} you will get the prefix defined in the WordPress installation, so always use this syntax to create the tables.

Even if you use the function $ wpdb-> query to create the tables it is advisable to use the function dbDelta since it compares the current table structure. It is not loaded by default so you must include it at the beginning of the file.

:: 4. Including Scripts and styles ::

Even if you only echo scripts and styles it is recommended to add the scripts using the function wp_enqueue_script . This function checks whether files and dependencies are available with other scripts. The following example shows how to use the function wp_enque_script :

First you use wp_register_style to register the style file and wp_enqueue_style to include the file. You must also indicate the path and unique identifier of the style file. Then the scripts are included using the function wp_enqueue_script . If it depends on other scripts you can mention them in the third parameter. In the example, a dependency of jQuery has been added.

Finally, you can add data that will be used within specific scripts using the function wp_localize_script . You can include the scripts where you want but always use the functions wp_enqueue_scripts and wp_enqueue_styles .

Also make sure to use the action admin_enqueue_script instead of wp_enqueue_script for the admin part.

:: 5. Creating shortcodes ::

The shortcodes are predefined code blocks that you can use anywhere. It is essential to learn how to use shortcodes if you want to be a plugin developer since they add dynamic elements to custom pages.

You can create shortcodes using a syntax like the following: