🎯 By the end of this lesson you can…
- Hook functions into actions
- Control order with priority and accept arguments
- Fire your own custom actions
🔌 WordPress PHP
actions.php
👆 Tap a line to explain it
1<?php2add_action( 'lesson_finished', function ( $name, $xp ) {3 echo "🎉 Well done, $name! +$xp XP\n";4}, 10, 2 );5 6add_action( 'lesson_finished', function ( $name ) {7 echo "📣 First: telling the teacher about $name\n";8}, 5 );9 10do_action( 'lesson_finished', 'Asha', 50 );11echo 'Fired ', did_action( 'lesson_finished' ), " time(s)\n";
| Hook | Fires when… |
|---|---|
init | WordPress has loaded — register post types here |
wp_enqueue_scripts | time to add CSS/JS on the front end |
admin_menu | building the dashboard menu |
save_post | a post is saved |
wp_footer | just before </body> |
rest_api_init | register REST routes |
🛠️ Order of greetings
Hook two callbacks onto an action called morning: one printing ☀️ Good morning with priority 5 and one printing 🥣 Breakfast time with priority 20. Then fire the action.
📌 Key takeaways
add_action( 'hook_name', 'callback', $priority, $args ).- Lower priority numbers run first (default 10).
do_action( 'my_plugin_event', $data )lets others extend your plugin.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.