What are plugins?
Plugins or modules are PHP files through which you can add content to pages and articles, add new features or modify existing ones by default in WordPress. Examples where you can use a module:
- Generating a data collection section (an integrated form or calendar);
- Modifying the content (changing the text of the “read more” buttons in the article feed or “add to cart” in the product pages);
- Improve themes (change default size for images), etc.
Why do I need a custom module?
It is true that in the “add mode” section of the WordPress menu we can download and use thousands of plugins for free. But we can face one of the following situations:
- Existing plugins may contain a lot of extra code or are simply not optimized and make it difficult to load pages where they are programmed to display content.
- There is no plugin to generate the desired functionality.
- We find many plugins that have similar purposes to our needs, but they do not look nice.
- Plugins are no longer updated by the developer.
- Plugins create conflicts with the theme or other essential modules that you do not want to give up.
For some developers it is easier to add a few lines of code than to use a module that is structured in a few hundred or even thousands of lines of code, which in fact offers a similar result.
What do you need to do before creating a module?
Before creating a module, I recommend testing it on a site on a local server or on a live site created specifically for testing. This way you can avoid situations where the code entered generates a fatal error, due to a code conflict (it could be overwriting), which can lead to the inactivity of the site for a short time. If you have a blog or an online store, displaying an error when opening the page will not work well in front of readers or customers.
If you need to implement extra features for your site and you want to create your own code, you will need to have some knowledge about WordPress hooks at the beginning. For example, if you want to create a code that modifies the text “read more” in the flow of your blog, you can create a function through which to modify this aspect by linking to the hook “the_content_more_link”.
The first step in creating your own plugin is to create a folder that will contain the files in which you will write the code. In this folder we will add a text file that we will call index.php. This file will of course contain our code, but first we will need to create a header to tell WordPress that this is a plugin, as well as information about the author, title and description of the mode, developer URL, etc. To create a header, we’ll enter the following code at the top of the index.php file we just created:
Plugin Name: Plugin URI: Description: Version: Author: Author URI: License:
You can customize this header by adding the module data after “:”, as follows:
- Plugin Name – The name of the plugin.
- URI plugin – If you are an experienced developer, you definitely have a website through which all the modules created are presented.
- Description – Description of the module.
- Version – Module version. If this is the first time you have created this type of code, it will most likely be something like 1.0.
- Author – Your Name
- Author URI – your website or social profile
- License – if any, mention the license obtained (eg GPL).
This data will also be displayed on the “Plugins” page in the module description section.
Enter the code
Once you’ve created the plugin header, you can start writing your own code, which you’ll include in a PHP function. Why in a position? After creating the lines of code, you will want it to be displayed somewhere on the site and do something. This can be done in two ways:
1. By creating a shortcode
You can use the add_shortcode function (param1, param2), where “param1” is the text that the shortcode will have, and param2 is the name of the function in which you created the plugin code. To display the content, use [“param1”] on the page.
2. By adding hooks to add action or filter.
For customization we can use for example one of the functions add_action (param1, param2) or add_filter (param1, param2) as the case may be, where param1 is the name of the hook we want to connect with, and param2 is the name of the function in which our code exists .
Once the code is ready, you can add your code to the WordPress site. Before doing this, the folder with the new plugin we created will need to be archived with the .zip extension. Next we will be able to upload the archived folder in two ways:
- directly in Cpanel or via FTP following the path public_html / wp-content / plugins / and unzip it in the “plugins” folder.
- from the WordPress Dashboard by selecting Module / Add Module from the main menu. At the top we will have the option to upload files.
After you have uploaded the plugin to the site, you will need to activate it from the list of installed modules.