Mahdi Mohebi

Hooks! the key of WordPress success

What is a hook?
The WordPress system uses a series of functions for its own more flexibility. With these functions you can make the desired changes in WordPress; And customize WordPress anywhere you want. You can also make changes to the WordPress core with these functions.

Keep in mind that plugins also use hooks to add a feature to WordPress; And they can add to their possibilities by hook. In general, WordPress is a comprehensive and flexible content management system. WordPress has a set of pre-prepared functions that sometimes we need to change how they work and this is easily possible by using hooks.

As you know, in WordPress, after updating the plugins and templates, all the changes you made in the main code of your template will be restored, so changing the core code of WordPress is not the right thing to do, this problem is solved with other hooks and you can Hook functions and commands to the main functions of WordPress; And your changes will last forever. Sometimes it is necessary to use templates and plugins to expand the site that you may not have access to, in which case you can use a hook.

Types of hooks in WordPress
Hooks are divided into two categories: action (filter) and filter (filter) and the difference in how it works, WordPress has 950 actions and 1450 filters.

action
Actions are used when you have created a function and want it to run at a specific time. For example, you want the function to run when the user logs in. This is where the action comes in handy with the help of a programmer and simply executes the desired functions at the required time. In fact, with Action Hook you can add a series of functions to the core of WordPress and change the features of your template and plugins.

For example, you can change the database information by action or create and send site pages to the user’s browser. You can also send emails with this feature.

Actions have many ready-made functions, one of which we see in the example below.

add_action (‘login_head’, ‘my_login’);

function my_login () {

echo ‘Enter username and password here’;

}
Filter
Filters are used when you want to make changes before running a function in WordPress. For example; Before displaying posts; Add content to the end of all posts. You can use the filter hook when you want to make changes to the output text.

In other words, you can make changes to the data before it is stored in the database. WordPress itself has all the filters by default, but you can customize these filters yourself; And filter the data as it exits.

For example, to change the function of the_content function that prints content; Create a function with the desired name as follows and hook it with the_content with add_filter, as follows:

add_filter (‘the_content’, ‘my_the_content_filter’);

function my_the_content_filter ($ content) {// Here we get a variable
$ content = “a b c”; // Here we change the content of the variable before printing
return $ content; // Then return the variable we changed

}

You may be wondering where we can find these action and suitable filters; The simple answer is to search for any hook you need in Google, such as: wp filter for echo post content, you will easily find the action or filter you need

What effect have actions and filters ever had?
Developers have written 100,000 free and paid plugins and templates using WordPress action and filters, and thousands of developers are already writing new plugins and templates with more interesting ideas for WordPress, and many plugins Pre-written templates and templates are constantly updated.

The capabilities that some of these plugins offer do not even cross your mind, and sometimes you can find very pure ideas in them that have not been repeated anywhere on the web.

These tools have entered all areas and you can almost rarely find an area that WordPress tools have not entered

As a result, WordPress has become the most popular web design cms, and many developers prefer to use frameworks like Laravel to build their own systems; Use WordPress cms.

Repeat the equation in WordPress plugins and templates
Question, if we can extend WordPress; Can we extend the WordPress plugin by writing addons? You mean, like, plug-in?

The answer is yes; This means that a plugin can easily use the WordPress system to define a new hook in its plugin, and then another plugin writer can write a new plugin (edan) for this person’s plugin.

For example, WooCommerce is a WordPress store plugin that many programmers write for WooCommerce, a plugin that enhances WooCommerce capabilities.

How scalable can this method be?
Using action and filters in WordPress, you can write infinite plugins in plugins! And in fact, with this simple method, WordPress can be taken anywhere! And used it to create any web system

in the end
WordPress showed how to create any system with a simple equation and a revolutionary method; Unlike the academic methods that Use the framework to create web-based systems

In my opinion, WordPress has challenged all web frameworks to the point that many of them are now very weak compared to WordPress.

Once I came across a .NET site and there was a discussion about site design, he first told me how he uses the Microsoft framework for site building and what the capabilities of this framework are, but when I was talking about how it works and I talked about creating new features in WordPress and showed him plugins that provide features that a .NET worker has to spend months of his life writing; He told me that if I knew the WordPress ecosystem was so flexible and had such good tools, I would have come to WordPress much sooner and put it at the core of my work.

In short, it is now up to many people to talk about the devil’s donkey and not waste their time with arbitrary frameworks and join the WordPress storm.

© 2019 All rights reserved.