Introduction to filters and actions

0

Since 2004, when the light world of WordPress 1.2 was seen, filters and actions have been among the cornerstones for the development of themes and modules.

Hooks (a general term for these two different groups of PHP functions) allow you to modify or extend the functionality of WordPress without having to interfere with the kernel files in any way. Below we will show that the filter changes the value of the content and the action can add another to the called function at the same time. Thanks to this, we have a very powerful tool for creating a unique blog or website, and at the same time we do not have to worry about problems updating to the latest version of WP.

The filter starts WordPress when you change text or other types of content before adding it to the database or sending it to the browser. Your module can specify that one or more PHP functions change a given type of content at a given time.


A simple example for understanding what is the basis of the hooks is a demonstration of an existing function and its modifications.

function get_excerpt($text, $length = 150) {

$excerpt = substr($text,$length);
return $excerpt;
}

 

This function contains two parameters: a string and the length to which we want to shorten it. Do you want two hundred characters in the post instead of the default 150 characters? No problem, we change the parameter when using the function. First of all, we apply a filter for the function that adjusts the length of the report.

 

function get_excerpt($text, $length = 150) {

$length = apply_filters('excerpt_length', $length);

$excerpt = substr($text,$length);
return $excerpt;
}

 

The name of this filter is excerpt_lenght and no function is attached to it yet, so the listing remains at 150 characters. So we have to tell the system that we want to “hook” the excerpt_length filter and define a new value. We will change this with the following entry:

 

function get_excerpt($text, $length = 150) {

$length = apply_filters('excerpt_length');

$excerpt = substr($text,$length);
return $excerpt;
}

function modify_excerpt_length() {
return 200;
}

add_filter('excerpt_length', 'modify_excerpt_length');

 

Done, the rule for all statements is that they will contain 200 characters. Without interfering with the original function, we have applied the filter to the length of the statement listing and the change will be reflected in each displayed statement.

The action triggers WordPress at specific points during command execution or when a specific event occurs. Your module can use an action to specify that one or more PHP functions will be executed at these points.

An example of a useful action is to insert a link to a page icon in the header.php file in your active theme. The function named theme_favicon contains HTML code with the address where the favicon.ico file is stored, writing add_action (‘wp_head’, ‘theme_favicon’); we tell the system that we want to append our function to the wp_head function by inserting the mentioned code between the tags< head>< / haed> .

 

function theme_favicon() { ?>

 <link rel="shortcut icon" href="<?php bloginfo('stylesheet_directory') ?>/images/favicon.ico"/>

<?php }

add_action('wp_head', 'theme_favicon');

 

The good news is that WordPress uses features everywhere and the editing options are essentially limitless, but novice developers could be put off by the number (currently over 1500). It’s not easy to find your way around so many features, but the official WordPress documentation includes well-crafted filter and action reports, and Adam R. Brown’s hook database is a good start to studying hooks.

 

Was this article helpful for you? Support me by sharing, please. 👍
WordPress Návod v PDF

LEAVE A REPLY

Please enter your comment!
Please enter your name here