On this page
- 1 WordPress: Hooks
- 2 WordPress: Hooks
- 2.1 Hooks
- 2.2 Example: save_post (Action Hook)
- 2.3 Example: the_title (Filter Hook)
- 2.3.0.1 Metadata in WordPress
- 2.3.0.2 WP_List_Table tutorial : How to display data from database to the admin block ?
- 2.3.0.3 Shortcode in WordPress
- 2.3.0.4 Add Tracking Code in WordPress
- 2.3.0.5 WordPress: Post Types
- 2.3.0.6 Creating an Administrator Account in WordPress Using Custom PHP Code
- 2.3.0.7 WordPress: Plugins
- 2.3.0.8 WordPress: Important Functions
- 2.3.0.9 Cheatsheet: WordPress
- 2.4 Leave A Comment Cancel reply
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.