How to create a custom WordPress plugin

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

WordPress plugins, also called extensions or modules, are an easy way to modify, customize, and enhance WordPress features. Instead of modifying the WordPress core, you can add functionality to it through plugins.
According to WordPress:

“ A plugin is a program, or a set of one or more functions, written in the PHP scripting language, that add a specific set of features or services to a “

In short, a plugin is essentially a piece of code that connects to WordPress. In human terms, that means a plugin is something that adds new functionality or extends existing functionality in WordPress.

What do you need to create a plugin in WordPress

While you don’t need to be an experienced programmer to develop your own WordPress plugins, basic knowledge of PHP, and .

Understanding WordPress Hooks or Hooks to be able to create a plugin in WordPress

Before we start getting into the matter of creating a WordPress plugin, I think it is necessary to understand what the .

Wouldn’t it be great to be able to tweak the way WordPress works to suit whatever project we’re working on? Well, that is what Hooks or hooks provide us.

WordPress includes Hooks (Hooks) that allow us to add our own code being the way in which the complements (plugins) and themes interact with the core of WordPress.

Let’s imagine that we are building a puzzle with more than 5000 pieces, because WordPress works in the same way, assembles each piece in its corresponding place through Hooks (hooks) and sends them to the browser to display them.

We have two types of Hooks, Actions Y Filters.

Actions:

Action Hooks allow us to load WordPress functions or create our own actions, for example we can use them to load CSS files, create an action when a button is pressed, display a message, etc…
Actions can be defined as follows:

add_action ( $hook, $funsction_name, $priority, $accepted_args )

  • $hook: Accepts an action name or the name of an action created by our template or plugin thanks to the do_action() function. In codex we can see a list:
    -> https://codex.wordpress.org/Plugin_API/Action_Reference
  • $fuction_name: Accepts the name of the function where the action is passed.
  • $priority: It is used to specify the order in which the action must be executed, this data is optional and by default it will always be 10, the lower that value is, the sooner that action will be executed.
  • $accepted_arg: It is used to specify the name of the arguments that are passed to the function $function_name, since the previous one is an optional data
See also  Product name in virtuemart 3.x too short

Example:

Suppose we have several employees using a company email and we want to remind them which email they have to use.
In this case it would be best to add a warning below the .

WordPress allows us to do this thanks to the use of an action hook.

When WordPress generates the content of the login form, it includes the following line of code:

do_action( ‘login_footer’ );

This means that we can attach a callback function to this action hook to render content on the page. For example, we could add a notice with the following code:

/* wordpress login footer notice */ function we_login_footer_message() { //our message in the login footer area ?>

Use your @domain.com email address to login here.

The result as you can see is a notice in the footer of the WordPress login:

Filters:

Filters in WordPress allow us to change data during WordPress execution. A callback attached to a filter will accept data in the form of a variable, make some changes to that data, and then return the variable.
Filters can be defined as follows.

add_filter ( $hook, $function_name ) $hook: Name of the filter $funtion_name: name of the function to which we pass that filter.

Example:

Let’s see how we can use a filter with a simple example.
If we look at the editing window of an entry we see that it shows us a text “Add the title”

If we review the WordPress code where it generates that text, we see that it loads it in the following way:

apply_filters( ‘enter_title_here’, __( ‘Enter title here’ ), $post );

Because the string “Enter title here” is executed through a filter (we can tell this because it is wrapped apply_filters())means we can change it, by attaching a callback function to the filter.

The code could be something like this:

function we_title_here( $title ) { // set a new text string. $title = __( ‘Enter article title here.’, ‘text-domain’ ); // return the title to the hook. return $title; } add_action( ‘enter_title_here’, ‘we_title_here’, 10 );

If we look at the code we see that we are assigning a new value to the variable $title ( $title = __( ‘Enter article title here.’, ‘text-domain’ ); ) that variable will be the one that will pass the value of the new data and the result would be this:

Surely you will think “OK Pepe all this is very good but when do we start with the plugin? “Well, we start right now. Ready? Come on, let’s stop theory and start having a little fun.

Create a WordPress plugin

In this example we will create a simple plugin that will allow us to disable options of the .

See also  Plagiarized duplicate content of my blogs to another website that is not...

The structure:

The structure of a plugin is made up of several files and folders, depending on the complexity of the plugin it can be simple with a couple of files or complex where we will already have a folder for images, js, css, Incluides….

The format would be the following:

  • plugin folder
  • Main plugin file
  • uninstall.php (file to uninstall the plugin)
  • /js: folder for javascript files
  • /css: folder for style files
  • /includes: folder for additional php files
  • /images: folder for the images that the plugin needs

prepare items

All are stored in the plugins folder found inside wp-content so the first thing we will do is create our plugin folder.
To create the folder and files we can use our WEPanel account:
->

We access the folder wp-content -> plugins and with the button on the right we select from the menu “create folder”

We will name the folder “ we_custom_products

Folder ” we_custom_products “ will be the one that stores all the plugin files so we will start by creating our main file.
Inside the folder “ we_custom_products ” that we have created we are going to create a file with the name ” we_cp.php
As we did to create the folder, with the button on the right we select “Create file -> PHP:PHP Code

We will name the file “ we_cp.php

The file ” we_cp.php “ will be the main file where we will have the header so that WordPress recognizes the plugin and we will call the other files.

We are going to give our plugin a bit of security by preventing external access to our plugin folder, for this we are going to create another file with the name index.php.

We edit the file selecting with the right mouse button and click on “ Edit

Inside we will add the following:

If any user tries to access the folder where we have our plugin, the first thing they will read will be the file index.php with the message “ silence is golden “ and the files inside that folder will not be displayed. We can add the message we want, even leave it blank.

Show the plugin in the administration.

In order for WordPress to recognize the plugin and show it in the plugins we will add a header in the file “ we_cp.php “ that we created earlier.
The header would be the following:

The only data that we need in the header for WordPress to recognize it is the name “ Plugin Name “, the rest of the data will be used to create the table with the data that will be displayed on the plugins page.

If we access the WordPress administration we can see what the plugin is showing, but of course even if we activate it it will not have any function since it is empty only with the header.

Let’s spice up the plugin a bit and add the functionality we’re looking for.

Add functionality to the WordPress plugin

For what we are looking for create WordPress plugin we will need two files, one for the backend and one for the frontend, these files, to have everything a bit controlled, we will create them inside the folder includes so the first thing will be to create the includes folder where we will store the files:

We access the includes folder and create two more folders with the name back-end Y front-end.

We access the folder back-end and we will create the file we-backend-init.php “ and we do the same for the Frontend, we access the folder and create the file “ we-frontend-init.php
Right now we already have our entire plugin structure that would be as follows:

  • we_custom_products
  • __we_cp.php
  • __index.php
  • __includes
  • __Backend
  • __we-backend-init.php
  • __Front end
  • __we-frontend-init.php

we_cp file

edit the file we_cp and add the following after the header:

if(!defined(‘ABSPATH’)){die(‘-1′);} function start(){ if(is_admin()==true){ require plugin_dir_path( __FILE__ ).’includes/Backend/we-backend- init.php’; } require plugin_dir_path( __FILE__ ).’includes/Frontend/we-frontend-init.php’; } function runit(){ add_action(‘init’,’start’); } runit();

If we review the code we see that we are only calling two files “ we-backend-int.php “ which is where we will add the codes for the plugin options in the admin and “ we-frontend-int.php “ .
The file ” we-backend-int.php ” It will only be loaded when we are in the administrator, for this we pass the parameter ” if(is_admin()==true)

Back end options

The next step will be to add the plugin options in the WordPress admin
Here, we are going to create a menu item in the WordPress admin panel which will allow us to access the plugin and allow us to choose some options to hide or show the elements on the WooCommerce product page.
We access the includes folder -> back-end and edit…

Loading Facebook Comments ...
Loading Disqus Comments ...