The basics of creating a plugin in WordPress-3

The basics of creating a plugin in WordPress.

The basics

At the heart of any plugin is PHP code, the logic of its work and the correct use of the WordPress API. The task of any plugin should be to create new functionality with the minimum possible load, compatibility with the WordPress core, and competent work of the code in a constantly updated system.

Plugin creation

The first thing you need to create a plugin is to create a separate directory (folder) for the plugin content, for example: my-plugin-name. This folder will contain all plugin files. Among them, a special place is occupied by the main plugin file, which, preferably, should coincide with the name of the plugin folder itself, for example, my-plugin-name.php. The result should be the following structure: wp-content / plugins / my-plugin-name / my-plugin-name.php.

After creating the plugin file, you need to put the headers in it so that wordpress recognizes the plugin as a plugin. To do this, you need to specify its name like this:

<? php

/ *

 * Plugin Name: My first plugin

 * /

After saving, the plugin can be seen in the list of plugins on the WordPress site. Log into your WordPress admin in the Plugins section (on the left in the admin navigation). You should see your new plugin there!

If the plugin is just one file like Hello Dolly, then it could be located directly in the plugins folder. But it is recommended to follow the standard: the plugin files must be in their own folder, and the main plugin file must have the name of the plugin folder.

A plugin can be private (created for only one site), or it can be public (for general use, uploaded to the WordPress plugin repository).

Requirements for a personal plugin are usually minimal, but with a public one it is more and more difficult, you need a license, support, compatibility, localization, and so on. Therefore, it is much more difficult to create public plugins.

The license tells users how they can use the plugin code for their own purposes. To maintain compatibility with the WordPress core, it is recommended that you select a license that works under the GNU General Public License (GPLv2 +).

There are other options in the header

The description of additional parameters will improve the display of the plugin in the WordPress dashboard. If you plan to submit your plugin to WordPress.org, you need to follow the WordPress plugin header requirements.

<? php

/ **

 * Plugin Name: Plugin name

 * Description: Description of the plugin, preferably not very long

 * Plugin URI: Link to plugin info

 * Author URI: Link to the author

 * Author: Author name

 * Version: The version of the plugin, for example 1.0

 *

 * Text Domain: Translation ID, specified in load_plugin_textdomain ()

 * Domain Path: Path to the translation file. Needed if the translation file is not in the same folder as the current file.

 * For example, the .mo file is located in the myplugin / languages ​​folder, and the plugin file is in myplugin / myplugin.php, then we specify “/ languages” here

 *

 * License: GPL2

 * License URI: https://www.gnu.org/licenses/gpl-2.0.html

 *

 * Network: Specify “true” to enable the plugin to be activated across all site networks (for Multisite build).

 * /

Template for WordPress plugin development

In order not to create files and structure from scratch, you can use the template to create a plugin:

See also: rating of starter themes and plugins.

The template is a standard and organized object-oriented framework.

The template adheres to the PHP Code Standards for WordPress.

Using this approach, you can be sure of a clearer and more understandable plugin structure. This way you can generate a base and then just delete all unnecessary files, leaving the folder structure – structure is important!

Hooks: Actions and Filters

There are many hooks throughout the WordPress core. Hooks allow you to connect in specific places to the WordPress core code to change its behavior without affecting the core files.

There are two types of hooks in WordPress:

  • events
  • filters

Events let you add or change WordPress functionality, while filters let you change data or rows. Hooks aren’t just for plugin developers – they are used everywhere: in the very core of WordPress, in plugins and themes. Hooks are what make WordPress so flexible.

There are three functions directly related to plugins that are responsible for plugin hooks:

  • register_activation_hook () – registers a function that will fire when the plugin is activated.Used to add plugin settings, etc.
  • register_deactivation_hook () – registers a function that should run after plugin deactivation.Used to remove temporary plugin data.
  • register_uninstall_hook () – registers a function that is called when a plugin is uninstalled.Used when uninstalling a plugin to delete all plugin data: in settings, in files, in a database, etc.

In addition to these three functions, the plugin API includes all hook functions and some plugin functions.

You can create your own hooks in the plugin source using do_action () or apply_filters (). They will allow developers to extend the plugin’s capabilities and make it extensible – just like the WordPress core.

Imagine that you have created a plugin and it is used by another developer, but at the same time you continue to develop the plugin, improve its code and release new versions of the plugin, but when you update all the previous plugin files are overwritten with the new ones. So, if another developer made edits directly to the files of your plugin, then his changes will be lost. To prevent this from happening, you need hooks, connecting to which another developer can extend your plugin without changing the code of the plugin itself.

Use the WordPress API

WordPress provides a number of APIs. APIs can greatly simplify writing code. Those. there is no need to reinvent the wheel – it already exists and has been improved 100 times.

Some APIs:

  • Settings API – makes it easy to create and manage plugin options that are saved to the database.
  • HTTP API – Simplifies making HTTP requests in PHP. Great replacement for bicycles with cURL.

How WordPress loads plugins

When activating a WordPress plugin, it writes the path to its main file in the active_plugins option. Further, when loading any page (admin and front), WordPress simply connects all the files from the active_plugins option (the paths to them are stored there as an array). See how it looks:

 

$ active_plugins = get_option (‘active_plugins’);

/ * Get in $ active_plugins

Array

(

[0] => hello-dolly / hello-dolly.php

[1] => backupwordpress / backupwordpress.php

[2] => democracy-poll / democracy.php

[3] => disable-emojis / disable-emojis.php

)

* /

It follows from all this: just by their presence, plugins do not affect the speed of the site (except for connecting the plugin file, which is a super fast operation). Inactive plugins have no effect at all. Read more here.

Leave a Comment

Your email address will not be published. Required fields are marked *