Яндекс.Метрика
qr-код - Как-то так стрелка - Как-то так
Leading Economist + ... пиктограмма - Как-то Так THIS     Economic literacy and web solutions
All on the topic: what, how and why. Actual things - in their own words.
Turn your quality WITH ME - BUTW !
If you don't like what you get, change what you give. © Carlos Castaneda     KAKTOTAK.BY - economic design and web development services (php/js - telegram, WordPress, Laravel, Vue, SEO)    Cooperation - on an ongoing basis    Be in the topic   -    SUBSCRIBE      My notes are ECONOMICS +  и  WEB Developer's Guide | php+js+seo   в Telegram   telegram  

The principle of add_action and do_action in WordPress is simple

The principle of add_action and do_action in WordPress is simple

Understanding the essence of two key functions - add_action and do_action - is fundamental for developing projects on WordPress, because they form the basis of the architecture of writing and outputting code on this platform.

WordPress has a powerful hook system that allows developers to add and change functionality without editing the main code. It is these two key functions in this system that solve these problems.

What is add_action?

add_action is a function that links your custom function to a specific WordPress hook. It is used when you want your feature to be triggered at a certain point in the WordPress lifecycle, for example, when loading a page or saving a post.

Example of using add_action:


function my_custom_message() {
    echo 'This is my custom message!';
}
add_action( 'wp_footer', 'my_custom_message' );

In this example, we create a my_custom_message function that outputs a message. Then we link this function to the wp_footer hook so that it runs at the bottom of the page.

What is do_action?

do_action is a function that calls all functions registered to a specific hook using add_action. You use this function in the place where you want to initiate the execution of all hook-related functions.

Example of using do_action:


function another_function() {
    echo 'This is a different function!';
}

// Adding it to our hook.
add_action( 'my_custom_hook', 'another_function' );

// Calling our own hook.
do_action( 'my_custom_hook' );

In this example, we add the another_function function to our own hook my_custom_hook. Then, when we call do_action('my_custom_hook' ), WordPress will perform all functions related to this hook.

Full example code

Now let's combine everything in one example, which can be placed in a file.functions.php of your WordPress theme:


// 1. Defining the function that will be called via wp_footer.
function my_custom_message() {
    echo 'This is my custom message!';
}

// 2. Add this function to the wp_footer hook.
add_action( 'wp_footer', 'my_custom_message' );

// 3. Define another function.
function another_function() {
    echo 'This is a different function!';
}

// 4. Add this function to our own my_custom_hook hook.
add_action( 'my_custom_hook', 'another_function' );

// 5. We call our own hook to display the message.
do_action( 'my_custom_hook' );

How does it work?

When you load a page, WordPress performs the following steps:

  1. When wp_footer is reached, it will execute the my_custom_message function, outputting the message: 'This is my custom message!'.
  2. Anywhere in the code where you call do_action('my_custom_hook' ), WordPress will execute all the functions added to this hook, including another_function, outputting: 'This is a different function!'.

The fundamental difference between these two entities is do_action and hooks

do_action is a function that calls all functions registered for a specific hook. It is not a hook in itself; rather, it is used to initiate the execution of functions linked to the hook.

The hook in question is the name that you pass as a string (for example, 'wp_footer' or 'my_custom_hook'). This is the hook itself, and this is the key name when you use the add_action and do_action functions.

Comparison:

do_action('my_custom_hook'): It's a challenge that means: "Perform all the functions that were added to the hook named 'my_custom_hook'."

add_action('my_custom_hook', 'my_function'): This is adding the function 'my_function' to a hook named 'my_custom_hook'. Here 'my_custom_hook' is the hook itself.

In other words:

Creating a hook:When you write add_action('my_custom_hook', 'my_function'), you create a link between the hook'my_custom_hook' and the function'my_function'.

Calling the Hook:When you write do_action('my_custom_hook'), you are calling all the functions that were associated with this hook. That is, if a function has been added to the 'my_custom_hook' hook somewhere, it will be executed when you call do_action.

That is:

do_action is a function for calling (holding) all functions associated with a specific hook.

Hook is the name of an extensibility point in WordPress to which functions can be linked.

Using add_action and do_action allows developers to create flexible and extensible solutions in WordPress themes and plugins. These are powerful tools that allow you to add functionality and change behavior without having to edit the source code.

автор - Михаленко Р.
M R. Автор - kaktotak.by Specialization: financial and economic design - modeling of business, investment projects of the real sector, analysis and evaluation of efficiency, optimization of the management decision system.

A wide range of web-based competencies for solving business problems.

Subscribe to my telegram channel - My notes are ECONOMICS +
Там я ничего не втюхиваю и не навязываю. Просто делюсь полезной информацией по экономической грамотности. Повышайте своё качество вместе со мной: что, как и почему в экономике на простом языке. Понятия, алгоритмы, процессы, микро- и макроаспекты и многое другое. Может, будет интересно !

And also - WEB Developer's Guide | php+js+seo
Notes and native solutions to simple local tasks in PHP, JS. Something about Laravel, WordPress, Vue and SEO.

  Personal assistance in economic design and web development:

  • Financial and economic modeling, analysis, accounting, business planning
  • Comprehensive web development/project support on the web (php/js, seo – Laravel, WordPress, Vue, telegram, administration, content, advertising in Yandex Direct

  telegram или форма обратной связи

Administrator
42
0
Name
E-mail
Rating
Review

Currency Converter
RUB RUB-icon
USD USD-icon
EUR EUR-icon
CNY CNY-icon
BYN BYN-icon
UAH UAH-icon
KZT KZT-icon
SHORT- what is it about
ECONOMIC LITERACY
  Simple online solutions to problems of economics and finance  
I Want To Know Everything. Useful Tips