Sunday, 4 January 2015

The basics - hooks, actions and filters

 There are two types of hooks: 
1. actions   and
2. filters.

Actions and filters allow you add your own functionality or modify your site’s behaviour by hooking a callback function onto a specific tag in the core code, setting priorities and using parameters and arguments passed to the callback function.

ACTION AND FILTERS DIFFERENCE :-


  • Actions allow you to add extra functionality at a specific point in the processing of the page—for example, you might want to add extra widgets or menus, or add a promotional message to your page.
  • Filters allow you to intercept and modify data as it is processed—for example, you might want to insert another CSS class in a WordPress HTML element, or modify some of Customizr’s page blocks.
In brief: actions do stuff; filters change stuff.

Getting down to details

When Gill comes by, tell her to go to the store to get some paint. When Jack comes by bragging about how great he is, get him to say that Gill is better.
Wait, what? I thought this was an article about actions, filters and hooks? Instead, we’re talking about arrogant home-improvers? Well yes. These are ways of understanding what’s going on when you use actions and filters.
Let’s look at actions and filters in turn.

Actions:

When Gill comes by, tell her to go to the store to get some paint.
We can do this by hooking onto an action hook. That is, we can use the  add_action function (written  add_action()) to add functionality—do stuff—at a particular point.
Sending Gill to the store to get paint might look like this in PHP:

/ Send Gill to get paint
add_action( 'after_gill_arrives' , 'send_gill_to_get_paint', 10 , 2 );
function send_gill_to_get_paint( $gill_has_keys, $gill_has_car ) {

// If $gill_has_keys and $gill_has_car are both true
if ( $gill_has_keys && $gill_has_car ) {
echo 'Gill, please go to the store and get some paint. Thank you!';
}
}

In simple terms,  add_action() tells WordPress to do something when it arrives at the specified  do_action() hook.

But we can only use our  add_action() in the example above if we know that the  'after_gill_arrives' action hook exists in our theme/plugin. Our theme/plugin needs to have something like this in it:
do_action( 'after_gill_arrives' , $gill_has_keys = true , $gill_has_car = true );



Filters:

// Cut Jack's boasting
add_filter( 'jacks_boast' , 'cut_the_boasting');
function cut_the_boasting($boast) {
// Replace "best" with "second-best"
$boast = str_replace ( "best" , "second-best" , $boast );
// Append another phrase at the end of his boast
$boast = $boast . ' However, Gill can outshine me any day.';
return $boast;
}


What did we just do there?


  • We looked for a particular thing that we wanted to change—Jack’s boast that he’s the best; in programming terms the  'jacks_boast' filter;
  • When we found it, we changed it—we changed “best” to “second-best” and we added another phrase on the end; in programming terms, we filtered the output by calling the  cut_the_boasting() function, which replaced part of the string and appended another string (the “.” character concatenates two PHP strings);
  • We used the  $boast argument (in this case a string that says something like “I’m the best!”) as the basis for our changes.
  • We returned a string at the end of the function. This is very important. If you don’t return a string in a filter, you might disrupt the functioning of a program (in this case, we would simply silence Jack … which may not be a bad thing).

How did it work?

In simple terms,  add_filter() tells WordPress to swap its data with ours when it arrives at the  'jacks_boast' filter hook. Neat!
But we can only use our  add_filter() filter in the example above if we know that the  'jacks_boast' filter hook exists in our theme/plugin. (If the hook doesn’t exist, our function will simply never be called.) Behind the scenes, the theme/plugin has to have something like this in it:
echo apply_filters('jacks_boast',"I'm the best in the world.");
This tells WordPress to create a hook called  'jacks_boast', apply any filters that are attached to this hook, and pass those filters the string “I’m the best in the world.”. If there are no filters attached to the hook, then the  apply_filters() function will simply return the string “I’m the best in the world.”: effectively a default.
// Change url that is linked from logo
add_filter( 'tc_logo_link_url', 'change_site_main_link' );
function change_site_main_link() {
return 'http://example.com';
}


Why use hooks?

  • Change almost anything in WordPress—even quite fundamental things—because a lot of WordPress’s core functions use actions and filters;
  • Make changes easily: once you’ve understood the concepts, you can make some incredibly complex changes very quickly;
  • Change Customizr’s behaviour at the source, rather than trying to retro-fit an inappropriate solution with HTML and CSS;
  • Make your own changes easy to understand and easier to debug, because your code is reduced to a minimum;
  • Enable and disable your changes easily because each piece of code is a small unit in your functions.php;
  • Make your changes relatively upgrade-proof because you no longer need to edit or copy WordPress’s or Customizr’s core files;
  • Share your knowledge and swap code snippets with others.

No comments:

Post a Comment