How to create a plugin in wordpress?

How to create a plugin in WordPress?


As is known, more than 30% of the websites in the world are created with WordPress. This means that with this web development framework more websites are generated on the planet than with any other software for this purpose. The diversity of sites that are developed with WordPress is equally wide. With this incredible tool you can create websites for any type of content. This versatility is possible precisely thanks to its status as a content management system (CMS). One of the features that most promote the use of WordPress is its plugins. The WordPress installation offers the basic foundational framework for a website. However, the functionalities can be easily extended and added by means of plugins. There are around 50,000 of them available at These separate pieces of code can be added in any installation without much difficulty. In this article we are going to know what plugins are, how they are integrated into WordPress, how they work, what their structure is, and create a wordpress plugin step by step. Before creating a wordpress plugin from scratch, we will explain some concepts.

What are plugins in WordPress?

In WordPress, plugins are pieces of code that are integrated into the system and extend or add functionality. These new capabilities offer flexibility to the basic installation, so that by mastering the skills to create them, it is possible to build virtually any type of application with WordPress. Plugins are one of the most important features of this framework. In fact, they are often underlined as the best reason to use WordPress. When created for this system, they need to be written in PHP, the programming language in which WordPress is developed. Through them it is possible to add to the system a great variety of characteristics that respond to the specific needs of the domain of almost any problem. The independent developers are the ones who create the plugins. These are reviewed at when they are uploaded, so that way you make sure that those who follow the guidelines stay and implement good programming practices from the WordPress repository. There are free and paid plugins, in the same way that happens with themes. The decision to choose a free plugin must take into account that you have to be careful when taking this step. With this category of plugins you can run the risk of not getting help or support of any kind. Payment plugins developed by recognized organizations typically ensure you get more features, functionality, and support. Plugins can:

    • add security to the site,
    • turn it into an e-commerce with cart,
    • enhance the management system functionalities for any type of content,
    • link it to the owner’s social networks,
    • make it look better,
    • improve their performance,
    • attract more traffic,
    • protect it from malicious programs, among other various possibilities.

When installing WordPress 2 plugins are automatically added. One of them is Hello Dolly, which shows fragments of the song of the same name, by Louis Armstrong, on the dashboard. It serves as an example of how the basic structure of the plugins should be in this framework. The other one is Akismet, a plugin that adds security to the site, keeping at bay the spam content that is sometimes added by bots. The plugins ensure that the functionalities that provide the installation of WordPress are maintained in the system even when changing the subject. As separate pieces of code that add to the workings of the WordPress core, they are modularly separated from it, and can be added or removed at any time. In a multi-site configuration, adding plugins to the parent installation shares the functionalities provided by this plugin for the installation network.

How do plugins work?

Plugins are supported by WordPress hooks. As explained in this post, hooks allow you to anchor custom code snippets to different parts of the WordPress core. In this way the developer can extend the core functionalities to make the system what he wants or needs. Hooks fall into two categories:

  • action hooks: allow to add or modify functionalities in WordPress
  • filter hooks: allow to redefine WordPress functionalities

The WordPress documentation on hooks contains all the information necessary to learn how to work with them. It is important to know most or at least the most important ones and when they are executed, in the case of a beginning developer. Plugins should be located in the / wp-content / plugins / folder, in their own directory, with the name determined by the developer. The name of the plugin directory must be unique so as not to conflict with another plugin that could have the same identifier. Underscores and underscores and spaces in the name of a plugin directory should be avoided. In turn, the main file should be named the same as the folder that contains it. A good strategy for shaping the identifier might be to match the name of the company or developer and a short description of the functionality it contains. For example, if the company is named WordPress Solutions and has created a moon travel plugin, the plugin folder could be identified as wps-moon-travel and the main file within the plugin would be named wps-moon-travel.php .

Another important consideration about plugins is security, as it does not provide vulnerable access mechanisms to the websites where the plugin is installed. Likewise, creating the plugin documentation is essential to offer the possibility for other developers to extend and improve the plugins that we conceive. Among the files that a plugin must contain, it is necessary to have at least one PHP file that would be the main one. The module could also contain JavaScript code files, CSS, images, other PHP libraries and files for the language. There is a bibliography that recommends the following configuration for files within the plugin (/ wp-content / plugins / my-plugin):

  • /adminpages /: The files that are the pages to display in the WordPress control panel for the plugin  are stored in this address
  • /classes /: The class definitions for the plugin are stored in this folder
  • /plugins / my-plugin / css /: The CSS styles included in this folder should be divided between admin.css for the administrative page and frontend.css for the page to be displayed to the user. In this folder you can include the CSS files that are needed to support a JavaScript library added to the plugin
  • /css/admin.css
  • /css/frontend.css
  • /js : JavaScript files that are necessary to provide functionality in the frontend to the plugin should be added in this folder. Likewise, here you can include or separate the designated JavaScript functions for each related party in admin.js and frontend.js files
  • /images /: Includes the images that the plugin will need
  • /includes /: Stores any type of PHP file that needs to be added to the plugin other than the main file, which must be the only file PHP at the root of the plugin. This can be the case of the functions.php , common.php or helpers.php files, which are almost always added as a standardized procedure in WordPress plugins. Small PHP scripts that accomplish simple plugin tasks can also be stored in this folder
  • /includes/functions.php
  • /includes / lib /: This incorporates third-party libraries that provide functionality for the plugin
  • /pages /: Store PHP files for the frontend
  • /services /: The PHP files for AJAX calls are located at this address
  • /scheduled /: Save PHP scripts related to scheduled tasks or features that should be executed at time intervals
  • /mi-plugin.php: This is the main plugin file. If the plugin’s functionalities require it, from a design point of view, it is possible to keep several files in addition to the main one, which would contain other functionalities, according to the design and the organizational needs of the developer. Normally in these cases the main file contains comments on the other files that would hold the code, in addition to include statements and constant definitions.

  • /my-plugin.php: Archivo principal PHP de la extensión
  • /uninstall.php: Archivo de desinstalación del plugin
  • /js/: Directorio para los archivos JavaScript
  • /css: Carpeta para los archivos de estilo CSS
  • /includes: Directorio para otros archivos del plugin como librerías
  • /images: Carpeta para las imágenes que usa el plugin

Keeping the plugin files organized in a clear hierarchy can make a big difference in productivity in the development of other plugin versions over time. In the same way you can help other developers to continue with the logic of the extension when they review the plugin code. Plugins can provide functionality that is useful or necessary for a WordPress installation. Each plugin can contain its own hooks, which would allow any developer to build extensions on top of other plugins. In the same way that WordPress hooks are used, it is possible to use the hooks of other plugins.

Code conventions and good practices

In WordPress the kernel is written in such a way that it follows certain standards to respect a style and keep the code readable, uniform, clean and consistent.

Plugin and theme developers should follow these standards when creating code for the platform as this maintains consistency between core code, plugins, and themes.

The standards can be consulted in the official WordPress documentation at

In general, it is necessary to document the code of the functions declaring a brief description, first, then a deeper description and finally expose the types, names and descriptions of the parameters and the value that is returned, if applicable. Likewise, variables, functions and files must follow a certain convention to be named, written in lower case, with words separated by underscores; and in the code use single quotes whenever possible.

To increase the readability of the code, it should be embedded in logical structures such as loops, conditions and functions, using the real tab and not spaces. Another recommendation to follow is to use the brace style in the code. This means that the opening parentheses must remain on the line, for example, of the conditional statement to which it belongs, and the closing parentheses on a separate line.

Commenting on the end of a very long code block is also good practice, because it makes it easier to read; And of course, the spaces in the code must follow the commas and exist on both sides of the logical or assignment operators. Something that is highly recommended to do when developing plugins is to establish a prefix that is taken into account to name any element that conforms to the extension. In the case that was presented, wrn should not only go in the name of the directory and the main file, but also in the names of the functions inside the plugin and the variable names that are used in the code.

This practice allows you to avoid surprises in the execution of WordPress, because without intention we could name a variable with the same identifier as another one in the core of the system and without a doubt that is not desirable. A good idea to make prefixes might be to use the initials of the programmer and the initials of the extension being developed.

To the previous conventions we must add the use of the standard PHP tags (<? Php to open a block of code or PHP file and?> To close), discarding the shortcuts <? and?> that can cause problems on many servers.

Creating a plugin for the first time

To develop a plugin you need to make sure that you can access the WordPress installation you are working with, specifically the directory where the plugins are stored, / wp-content / plugins /.

It is recommended to work with a test installation, in order to protect any production system from possible errors that arise during the development of the plugin.

In the folder where the plugins are stored, create the directory that will contain the plugin that will be developed. It will be named as the plugin. In our case, a very simple plugin will be created in order to learn the basic and fundamental steps when generating new plugins. We will focus on the good practices that it is recommended to follow in the plugin creation process. The first thing to consider is the consistency of the name of the plugin directory with the plugin itself.

As already explained, it is necessary to name the folder that stores the plugin in the same way as the main file of the plugin. Although it is true that you can create a plugin that works with a simple PHP file, directly contained in the plugins folder, it is best to create a directory that contains the main file and other elements that may need to be added in the development process. of the same. The extension we will create will simply take the content of the excerpt of the entries and change them for a short text.

Given the fact that our goal is to learn the basics of creating plugins, there is no need to implement something complex. Making use of the hook facilities to add a little example functionality is enough. We will name the folder that contains the plugin with the identifier wrn-my-first-plugin, and the main file, if we follow the good practices, will be designated as wrn-my-first-plugin.php. Once the plugin directory and its main PHP file have been created, we proceed to edit the latter. In this step it is important to know that the main file of a plugin must contain certain information related to it, that WordPress shows the user who installs the extension on their website. This information is declared within a multiline comment at the beginning of the main file. The plugin header is the only requirement that exists for it to work in WordPress.

This block indicates to the system that the extension is valid. At least the plugin name must be added in the header, so that it can be recognized by the framework. However, it is good practice to include the other elements that make up the block. The following code snippet is the complete example of our plugin:

     * Plugin Name: WebReadyNow - My first plugin
     * Plugin URI:
     * Description: This is a test plugin to show how importan they are
     * Author: Carlos Ernesto Velazquez Rodriguez
     * Version: 1.0.0
     * Author URI:
     * License: GPL-2.0+
     * License URI:

    #region Archivo: /wp-content/plugins/my-plugin/my-plugin.php

     * Return predifined text
     * this function return html block
     * @return  html block
    function wrn_my_filter_hook_code( ) {
        $wrn_excerpt_text = '<i>Custom code linked to  \'the_excerpt\'</i> hook<br><br>';
        return $excerpt_text;

    // Filter Hook
    add_filter('the_excerpt', 'wrn_my_filter_hook_code');

The elements included in the plugin header are described below:

  • Plugin Name: Plugin’s name
  • Plugin URI: The address where the plugin can be found
  • Description: Plugin’s description
  • Version: Plugin’s version
  • Author: Plugin’s author name
  • Author URI: Plugin’s author url
  • License: Plugin’s license

The functionality of the plugin that is presented is very simple. It consists of changing the summary of the posts by the text Custom Code anchored to the filter hook ‘the_excerpt’, showing it in italics. When activating the plugin, every time the_excerpt() function is called in the active theme or template, instead of the text that is stored in the database for each abstract of each article, the text shown above will be displayed.


WordPress plugins are a powerful tool as they add developer-created functionality to the core of the system, enriching and adapting it to the needs of different and broad groups of software developers. This versatility makes WordPress the adaptable system it is, which, among other benefits, allows this CMS to enjoy the popularity it has to date, making it the most widely used framework for creating applications and websites throughout the world. planet. The plugins in this system are located in the / wp-content / plugins / folder, and it is where they must be copied directly so that they appear in the Plugins section, where you will have the possibility to install, uninstall, activate, deactivate or eliminate them on the platform. A plugin must minimally contain a main PHP code file with its name. Ideally, all extensions should include within a directory all the files that make them up, such as images, CSS style files or JavaScript codes. A multi-line comment block must be included in the header of the main plugin file. In this section it is necessary to specify at least the name of the plugin so that WordPress recognizes it as such. Other information that should be added for good practice are the description of the functionalities that the plugin solves, the name of the author, a reference web address of the plugin and the author, the version of the plugin and the name of the license under which it is used. create the plugin. Plugins work by taking advantage of the hooks provided by the WordPress core, and in turn they can register new hooks themselves to allow other plugins to extend their functionality and further enrich the system. Like all well written code, WordPress plugins must follow defined standards to maintain a certain homogeneity, readability and consistency. From that approach, WordPress extensions must follow the code standards that WordPress core itself follows. We hope this article has helped you get started in the world of WordPress plugin development, which is a first step in creating systems based on your own needs or those of your future clients. If it has served you, share it on your social networks to help others learn more about this incredible system that is WordPress.