Wordpress Hooks | Adrenalina

WordPress Hooks

Publicado Por | 14 junio , 2013 | Blog Adrenalina En, Technical SEO | No Comments

wordpress hooks

If you’ve sometimes wondered how a WordPress plugin works like, the most common reply is: using hooks. They’re also used a lot to customize themes.

What are “hooks”?

Hooks are a WordPress function which allows us to, broadly, add or edit the usual behavior it has without having to edit the source code.

Hook types

We’ve got 2 types:

  • action hooks
  • filter hooks

Action hooks

Action hooks are applied by WordPress in some specific spots during execution or when a specific event happens. Thus we can define functions which will execute on that moment.
Action hooks are usually used for:

  • database data modifying
  • send an e-mail
  • modify the front-end or the admin desktop.

Example

We want our Wodpress to send an email whenever a new post is uploaded. We’ll create this function:

<?php
function email_me($post_ID)  { 
wp_mail( 'me@example.net', 'The subject', '<p>new post with id: </p>'.$post_ID ); 
return $post_ID;
?>

And we apply the hook we want to use:

add_action('publish_post', 'email_me');

Filter hooks

Filter hooks are used to modify different types of text before adding them to the DB or send them to the browser.

Example:
We want the outgoing mail to be recognized as HTML. So…

<?php
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( 'me@example.net', 'The subject', '<p>The <em>HTML</em> message</p>' );
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type(){
return 'text/html';
}
?>

Deactivate filters or actions

Sometimes we can find the need to deactivate some action or filter, be it because a plugin has been add or to avoid conflicts (as we saw on the filter example). So we’ll use these:

remove_filter('filter_hook','filter_function') ;

o

remove_action('action_hook','action_function');

Hook Priority

Sooner or later all WP developers reach a situation in which various plugins edit the same hook causing some problem. To avoid those situations we must make use of the hook’s priority to order their execution. A hook, by default, has a priority of 10, if we reduce that it’ll be applied before those with a higher value.

add_action ( 'hook_name', 'your_function_name', [priority]);

Reusing hook functions

One of the good aspects this system has is how easy it is to reuse functions we’d previous defined or even outside WordPress, so you only need to copy/paste the code.
Then we’ll only have to add the hook so that WP knows where to apply it.

Autor: mauro flores

Estudiamos y analizamos su negocio en profundidad, definimos objetivos y planteamos la estrategia de marketing más adecuada centrándonos en conseguir cada uno de los objetivos propuestos. Solicita Presupuesto Ahora

Uso de cookies

En este sitio web utilizamos cookies propias y de terceros para mejorar nuestros servicios, para que usted tenga la mejor experiencia de usuario y analizar su visita. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.