📖 Scenario: You are building a simple WordPress plugin to customize how your site works. You want to add a message to the footer and change the title of posts before they show on the site.
🎯 Goal: Build a WordPress plugin that uses add_action to add a footer message and add_filter to modify post titles.
📋 What You'll Learn
Create a function called custom_footer_message that echoes a footer message
Use add_action to hook custom_footer_message to the wp_footer action
Create a function called modify_post_title that adds ' - Custom' to the post title
Use add_filter to hook modify_post_title to the the_title filter
💡 Why This Matters
🌍 Real World
WordPress developers use add_action and add_filter to customize themes and plugins safely and flexibly.
💼 Career
Understanding hooks is essential for WordPress plugin and theme development jobs.
Progress0 / 4 steps
1
Create the footer message function
Create a function called custom_footer_message that echoes the text 'Thank you for visiting!'.
Wordpress
Hint
Use the function keyword to create custom_footer_message. Inside, use echo to show the message.
2
Hook the footer message function to wp_footer
Use add_action to hook the custom_footer_message function to the wp_footer action.
Wordpress
Hint
Call add_action with the first argument as 'wp_footer' and the second as 'custom_footer_message'.
3
Create the post title modification function
Create a function called modify_post_title that takes one parameter $title and returns the title with ' - Custom' added at the end.
Wordpress
Hint
Define modify_post_title with $title as input. Use return to add ' - Custom' to the title.
4
Hook the post title modification function to the_title filter
Use add_filter to hook the modify_post_title function to the the_title filter.
Wordpress
Hint
Call add_filter with 'the_title' as the first argument and 'modify_post_title' as the second.
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]