
Après pas mal de recherche sur le net, j’ai trouvé de nombreux articles les hooks WordPress mais peu d’article qui donne des explications satisfaisantes. Cet article est là pour vous donner une définition des hook, des exemples pour comprendre comment ces fonctions interagissent entre elles.
Hook définition :
En anglais, hook signifie crochet ou hameçon. Plus précisément, un hameçon est une pointe en fer qui est accroché au d’un fils en nylon, lui-même attaché à une canne à pêche qui va permettre d’attraper un poisson.
En terminologie WordPress, un hook WP est une fonction qui va permettre de modifier une action existante grâce à un comportement nouveau. Si je continue sur la métaphore de la pêche :
- le hook WordPress va jouer le rôle de la pointe en fer qui va relier
- le comportement nouveau peut être considéré comme le fil, la canne à pêche
- l’action existante qui peut être considéré comme le poisson
Hook WordPress, comment ça marche :
Pour mettre en place un hook, il faut utiliser add_action()
La syntaxe de add_action est la suivante :
<?php add_action( $tag, $callback_function, $priority, $accepted_args ); ?>
Si je continue sur la métaphore de la pêche, voici une représentation alternative de la fonction add_action :
Et do_action() ?
…
Liste des actions existantes fréquemment hookées :
Front Office :
tag action | description |
---|---|
wp_head | |
wp_footer | |
the_title | |
exemple utilisation :
add_filter( 'the_title', function( $title ) { return '<b>' . $title . '</b>'; } ); |
|
comments_open | |
exemple utilisation :
function wpc_comments_closed( $open, $post_id ) { $post = get_post( $post_id ); if ('page' == $post->post_type) $open = false; return $open; } add_filter('comments_open', 'wpc_comments_closed', 10, 2); |
|
Backoffice :
tag action | description |
---|---|
admin_menu | |
exemple utilisation :
function remove_menus(){ if(get_current_user_id()<>1){ remove_menu_page( 'index.php' ); //Dashboard remove_menu_page( 'edit.php' ); //Posts remove_menu_page( 'upload.php' ); //Media remove_menu_page( 'edit.php?post_type=page' ); //Pages remove_menu_page( 'edit-comments.php' ); //Comments remove_menu_page( 'themes.php' ); //Appearance remove_menu_page( 'plugins.php' ); //Plugins remove_menu_page( 'users.php' ); //Users remove_menu_page( 'tools.php' ); //Tools remove_menu_page( 'options-general.php' ); //Settings } } add_action( 'admin_menu', 'remove_menus' ); |
|
wp_before_admin_bar_render | |
exemple utilisation :
function remove_admin_bar_links() { if(get_current_user_id()<>1){ global $wp_admin_bar; $wp_admin_bar->remove_menu('comments'); // Remove the comments link $wp_admin_bar->remove_menu('new-content'); // Remove the content link } } add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' ); |
|
wp_dashboard_setup | |
exemple utilisation :
function kill_all_dash_widgets() { global $wp_meta_boxes; //Widget de Press Minute unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']); unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']); //Widget des liens entrants unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); //Widget des statistiques: nombre d'articles, de pages, etc. unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); //Widget des news sur les plugins WordPress unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); //Widget de brouillons recents unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']); //Widget des commentaires recents unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']); //Widget des infos "Blog WordPress" unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); //Widget des autres actualités de wordpress unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); } add_action( 'wp_dashboard_setup', 'kill_all_dash_widgets' ); |
|
manage_posts_columns | |
exemple utilisation :
function remove_comments_columns($columns) { // Remove comments column unset($columns['comments']); unset($columns['wm-thumb']); return $columns; } add_filter('manage_posts_columns', 'remove_comments_columns'); |
|
Trouver les hooks utilisables :
….
Assez claire ! Merci pour le tuto
do the ‘hooks’ for actions and filters overlap? -can I add a either a filter or an action to the same hook?
Hello,
For me, there is two kind of hook in wordpress : action hook and filter hook.
– Action Hook allow the developper to add new instruction on a behaviour of wordpress (or native function of wordpress) at special moment such as adding (it is the same behaviour in OOP when you extend a class then call the parent method and add instructions)
when you want to add a new behaviour (= a new action) wordpress give a function : add_action(hook_name,ref_function)
Filter Hook allow the developper to modify the result of a native function of wordpress which return a string, an array and so on.
when you want to add a new filter, wordpress give a function : add_filter(hook_name,ref_function)
For me, wp_enqueue_script() and wp_enqueue_style() are filter hook because you add new css or js files.
So does « actions and filters hooks overlap » ? they are more parallel.
« can I add a either a filter or an action to the same hook » ? no for me a filter hook and a action hook have different hook name so you can’t use for the same hook name add_filter() or add_filter(). It depends if the hook_name function return or not a value.
I hope this is more explicit.