In WordPress, hooks allow developers to:
Think about how you can safely customize WordPress without breaking updates.
Hooks let you add or change features without touching WordPress core files, keeping updates safe and easy.
Consider this code snippet:
add_action('init', 'my_custom_function');
function my_custom_function() {
error_log('Init hook fired');
}What is the behavior when WordPress runs?
Think about when the 'init' hook is triggered in WordPress.
The 'init' action runs on every page load during WordPress initialization, so the function logs the message each time.
You want to add a filter to change all post titles to uppercase. Which code is correct?
function uppercase_title($title) {
return strtoupper($title);
}Filters modify data, actions run code. The hook name must be exact.
'the_title' is the correct filter hook to modify post titles. Using add_filter with the correct hook name applies the change.
Review this code:
add_filter('the_content', 'add_signature');
function add_signature($content) {
$content .= ' - Thanks for reading!';
}Why does the post content remain unchanged?
Filters must return the modified value to apply changes.
The function modifies $content but does not return it, so WordPress uses the original content unchanged.
Order these hooks as they run during a typical WordPress page load:
Think about plugin loading, theme setup, initialization, then final loading.
First plugins_loaded runs after plugins load, then after_setup_theme runs to set up the theme, then init runs for initialization, and finally wp_loaded runs when WordPress is fully loaded.