🔌 Actions Do Something. Filters Change Something.
Both hooks, but different purposes. Actions execute code. Filters modify data. Knowing the difference saves hours of debugging.
⚡ Action Hook
// Do something (no return value)
add_action('wp_head', 'add_google_analytics');
function add_google_analytics() {
echo '<script>// GA code</script>';
}
// do_action() executes callbacks
// Returns nothing, just runs
🔄 Filter Hook
// Change something (must return value)
add_filter('excerpt_length', 'custom_excerpt_length');
function custom_excerpt_length($length) {
return 50; // MUST return value
}
// apply_filters() passes value through callbacks
// Returns modified value
📝 Common Mistakes
// ❌ WRONG: Forgetting to return in filter
add_filter('the_title', 'modify_title');
function modify_title($title) {
$title = $title . ' - Modified';
// missing 'return $title' → null!
}
// ✅ CORRECT: Always return
function modify_title($title) {
return $title . ' - Modified';
}
// ❌ WRONG: Returning in action (useless)
add_action('wp_head', 'add_meta');
function add_meta() {
return '<meta name="author" content="John">';
// Return value ignored!
}
// ✅ CORRECT: Echo or do work
function add_meta() {
echo '<meta name="author" content="John">';
}
✅ Priority Examples
- Lower number = earlier execution (default 10)
- Use priority 1 for first, 999 for last
- add_action(‘init’, ‘my_func’, 20); // after default (10)
- add_filter(‘the_content’, ‘my_filter’, 5); // before most
“Filter wasn’t working. Forgot to return value. Debugged for 2 hours. Actions and filters are simple but unforgiving. Return or echo, choose wisely.”
