LESSON 02 / 04

Actions

Actions let your code run at specific moments — when WordPress starts, when a post is saved, when the footer prints. Add them, set priorities and even create your own.

🎓Mediumlevel
⏱️35 minto finish
✏️1activities

📂 Plugins & Hooks🏷️ WordPress

🎯 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";
Popular actions
HookFires when…
initWordPress has loaded — register post types here
wp_enqueue_scriptstime to add CSS/JS on the front end
admin_menubuilding the dashboard menu
save_posta post is saved
wp_footerjust before </body>
rest_api_initregister 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.