Hooks in WordPress are functions that allow developers to modify or add functionality to WordPress without changing the core files.
There are two main types of hooks:
A. Action Hooks
Action hooks are used to execute a function at a specific point in WordPress.
Common Action Hooks:
initwp_loadedwp_headwp_footeradmin_initadmin_menuadmin_enqueue_scriptswp_enqueue_scriptssave_postpublish_postlogin_inituser_registercomment_postwidgets_initafter_setup_theme
Example:
function my_function() {
echo "Hello WordPress";
}
add_action('wp_footer', 'my_function');
B. Filter Hooks
Filter hooks are used to modify or change existing data before it is displayed or stored.
Common Filter Hooks:
the_contentthe_titlethe_excerptthe_permalinkthe_authorwp_titlebody_classpost_classexcerpt_lengthexcerpt_moreupload_mimeslogin_redirectcomment_textwidget_textdocument_title_parts
Example:
function change_title($title) {
return $title . " - My Website";
}
add_filter('the_title', 'change_title');
Difference
| Action Hook | Filter Hook |
|---|---|
| Performs an action | Modifies data |
| Does not normally return a value | Must return the modified value |
Uses add_action() | Uses add_filter() |
Example: wp_footer | Example: the_content |
In short:
Action = Do something
Filter = Change something