What are the hooks in WordPress?
WordPress Hooks allows the plugins to ‘hook into the rest of the WordPress plugin that you call at specific times, hence your plugin set in motion. These are the two types of hooks:
- Action hooks: Action hooks refers to facilitating you to insert an additional code from an outside resource.
- Filter hooks: Filter hooks refers to facilitating you to add content or text at the end of the post.
🔁 1. Actions
-
Purpose: To execute custom functions at specific points in the WordPress lifecycle (e.g., when a post is published, when a theme is loaded).
-
Does not return a value — it’s used for side effects, like sending an email, enqueueing a script, etc.
Example:
🔄 2. Filters
-
Purpose: To modify data before it is sent to the browser or saved to the database.
-
Must return a value, usually a modified version of the input.
Example:
📌 Common Hook Examples
Actions
-
init
– Runs after WordPress is loaded, but before any headers are sent. -
wp_enqueue_scripts
– Used to load CSS and JS files. -
save_post
– Triggers when a post is saved.
Filters
-
the_content
– Modify post content. -
excerpt_more
– Customize the “read more” text. -
upload_mimes
– Allow more file types for upload.
🧩 How to Use Hooks
-
add_action('hook_name', 'your_function_name');
-
add_filter('hook_name', 'your_function_name');
You can also remove or modify existing hooks with:
-
remove_action()
-
remove_filter()