add_action runs your function at a specific event.
add_filter changes data by passing it through your function.
Examples
This adds a message at the bottom of every page.
Wordpress
add_action('wp_footer', 'show_footer_message');
function show_footer_message() {
echo '<p>Thanks for visiting!</p>';
}
This changes every post title by adding 'Hello:' before it.
Wordpress
add_filter('the_title', 'add_prefix_to_title');
function add_prefix_to_title($title) {
return'Hello: ' . $title;
}
This runs your function early when WordPress starts.
Wordpress
add_action('init', 'custom_init_function', 10, 0);
function custom_init_function() {
// Code to run early in WordPress loading
}
Sample Program
This code adds a blue welcome message centered in the footer of every page. It also adds a star before every post title.
Wordpress
<?php
// Add a message to the footer
add_action('wp_footer', 'footer_message');
function footer_message() {
echo '<p style="text-align:center; color:blue;">Welcome to my site!</p>';
}
// Change post titles by adding a star
add_filter('the_title', 'star_title');
function star_title($title) {
return'⭐ ' . $title;
}
?>
OutputSuccess
Important Notes
Always use unique function names to avoid conflicts.
Priority controls the order your function runs; lower numbers run first.
Filters must return the modified data; actions do not return anything.
Summary
add_action lets you run code at specific WordPress events.
add_filter lets you change data before WordPress uses it.
Use these to customize WordPress safely without changing core files.
Practice
(1/5)
1. What is the main purpose of add_action in WordPress?
easy
A. To create new user roles
B. To change the content of posts before display
C. To register new database tables
D. To run custom code at specific WordPress events
Solution
Step 1: Understand what add_action does
add_action hooks your custom function to WordPress events so it runs at the right time.
Step 2: Compare with other options
Changing content before display is for filters, not actions. Registering tables or roles is unrelated to add_action.
Final Answer:
To run custom code at specific WordPress events -> Option D
Quick Check:
add_action runs code on events = D [OK]
Hint: Actions run code at events; filters change data [OK]
Common Mistakes:
Confusing add_action with add_filter
Thinking add_action changes data directly
Assuming add_action registers database or roles
2. Which of the following is the correct syntax to add a filter in WordPress?
easy
A. add_filter(my_function, 'the_content');
B. add_action('the_content', 'my_function');
C. add_filter('the_content', 'my_function');
D. add_action(my_function, 'the_content');
Solution
Step 1: Recall the correct order of parameters for add_filter
The first parameter is the filter hook name as a string, the second is the callback function name.
Step 2: Check each option's parameter order and function used
add_filter('the_content', 'my_function'); uses add_filter with correct parameter order. Options B and D use add_action incorrectly. add_filter(my_function, 'the_content'); reverses parameters.
Final Answer:
add_filter('the_content', 'my_function'); -> Option C
Quick Check:
Filter syntax: add_filter(hook, function) = A [OK]
Hint: Filter syntax: hook name first, function second [OK]