On this page
WordPress: Hooks
WordPress: Hooks
January 28, 2020
Hooks
WordPress hooks are predefined points in the WordPress core code where developers can use them to inject an additional code or modify a variable. WordPress hooks have two types of hooks:
- Actions
- Filters
Action | Filters |
Actions allow you to add data or change how WordPress operates. Callback functions for Actions will run at a specific point in the execution of WordPress and can perform some kind of a task, like echoing output to the user or inserting something into the database. | Filters give you the ability to change data during the execution of WordPress. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. |
Filters are used to modify variables. Unlike actions, filters code must return a value, which is the modified copy of the original value. |
Example: save_post (Action Hook)
Fires once a post has been saved.
<?php add_action("save_post", "callback_method"); function callback_method( $post_id ){ // You can add any code here and it will be executed when the user save s a post. } ?>
Example: the_title (Filter Hook)
<?php add_filter("the_title" , "capitalize_post_titles" ); function capitalize_post_titles( $post_title ){ $title = ucwords( $post_title ); return $title; } ?>
admin_post_action
https://developer.wordpress.org/reference/hooks/admin_post_action/
WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime.