WordPress hooks are a way to let developers extend, customize, or modify WordPress behavior without editing core files. Hooks are heavily used in WordPress core, themes, and plugins. WordPress has two main hook types: Actions and Filters.
What is a Hook?
A hook is a specific point in WordPress where custom code can run.
For example, you can use hooks to:
- Run code after a post is published
- Add custom CSS or JS in the header
- Modify email content before sending
- Add extra fields to a form
- Change plugin output
Types of WordPress Hooks
1. Action Hook
An action hook runs custom code at a specific time. It does not usually return a value. Actions are used when you want to “do something.” WordPress describes actions as hooks that run at specific points during execution.
Example
add_action('wp_head', 'nextcrm_add_custom_header_code');
function nextcrm_add_custom_header_code() {
echo '<meta name="nextcrm" content="enabled">';
}
Here, the function runs inside the WordPress site header.
2. Filter Hook
A filter hook modifies data and returns the changed value. Filters are used when you want to “change something.” WordPress filters allow plugins to modify internal data at runtime.
Example
add_filter('the_content', 'nextcrm_add_text_after_content');
function nextcrm_add_text_after_content($content) {
$content .= '<p>Thanks for reading.</p>';
return $content;
}
Here, the post content is modified before displaying on the frontend.
Difference Between Action and Filter
| Hook Type | Purpose | Return Value | Example Use |
|---|---|---|---|
| Action | Run extra code | No return required | Send email after form submit |
| Filter | Modify data | Must return value | Change email subject or content |