In WordPress development, theHooksA core mechanism for achieving a high degree of customization and functionality extensibility, WordPress provides two main types of hooks:Action Hooks and Filter Hooks. Among themadd_action Used to add action hooks, which allow you to execute custom functions at specific moments in the WordPress runtime.
![Image [1]-WordPress add_action hooks in detail: examples of use and best practices](https://www.361sale.com/wp-content/uploads/2025/04/20250417110133222-image.png)
This article will detail how to use the add_action hooks, and lists some common application scenarios and hook examples.
What is it? add_action Hook?
In WordPress.hooksare scheduled points for performing specific functions. When WordPress performs certain actions, developers can utilize hooks to insert theCustom CodeTheadd_action is one of the hook types that are used when you trigger certain events on theCustom Functions.
utilization add_action When you do, you need to specify:
- Hook NameThe name of the event.
- Callback FunctionA custom function that is executed when the event is triggered.
- Priority(Optional) Determines the order in which hooks are executed; the default is 10.
- Arguments (Accepted Arguments)(Optional) Defines the number of parameters passed to the callback function.
![Image [2]-WordPress add_action hooks in detail: examples of use and best practices](https://www.361sale.com/wp-content/uploads/2025/04/20250417195949563-image.png)
basic usage
Defining Callback Functions: First, you need to define a callback function. This is the code that the system will execute when a hook is triggered. For example
add_action The basic syntax is as follows:
php
add_action( 'Hook name', 'Callback function', priority, number of arguments );
Example:
php
function my_custom_function() {
echo 'Hello, World!
}
add_action( 'wp_footer', 'my_custom_function' ); }
In this example, when WordPress executes the wp_footer hooks are called when the my_custom_function function at the bottom of the page and outputs the Hello, World!The
common add_action Hook Example
1. wp_head
wp_head is a common hook in WordPress themes that allows the developer to add a new hook to the page's <head> Partially inserts custom content. Generally used to insert CSS, JavaScript, Meta tags, etc.
php
function add_custom_css() {
echo ''; }
}
add_action( 'wp_head', 'add_custom_css' ); }
2. wp_footer
wp_footer is another common hook, which when triggered is usually located in the page's section. You can insert custom JavaScript, analyze code, etc.
php
function add_tracking_code() {
echo ''; }
}
add_action( 'wp_footer', 'add_tracking_code' ); }
3. init
init The hook is at one of the earliest points in time after WordPress has finished loading. You can use this hook to initialize plugin settings, register custom post types or taxonomies, etc.
php
function my_plugin_init() {
// Register a custom post type
register_post_type( 'my_custom_post_type', [
'labels' => [
'name' => 'My Custom Post Types',
'singular_name' => 'Custom Post Type'
], 'public' => true, 'post_type', 'custom_post_type'
]);
}
add_action( 'init', 'my_plugin_init' );
4. admin_menu
admin_menu Hook for adding custom menu in WordPress admin backend. This hook is usually used for plugin development to help developers create admin pages.
php
function my_custom_menu() {
add_menu_page( 'My Custom Page', 'Custom Menu', 'manage_options', 'custom-page', 'my_custom_page_function' );
}
add_action( 'admin_menu', 'my_custom_menu' ); }
function my_custom_page_function() {
echo '<h1>Welcome to My Custom Page!</h1>';
}
5. save_post
save_post is a very useful hook that is triggered when saving a post. You can use this hook to perform actions such as automatically saving custom fields or performing other logic.
function my_save_post_action( $post_id ) {
function my_save_post_action( $post_id ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id ;
if ( get_post_type( $post_id ) ! = 'post' ) return $post_id;
// Perform the customized action
update_post_meta( $post_id, '_my_custom_meta_key', 'Some custom value' ); }
}
add_action( 'save_post', 'my_save_post_action' ); }
Setting Priorities and Parameters
1. prioritization
add_action Hooks allow you to specify a priority for each callback function. The default priority is 10. By adjusting the priority, developers can control the order in which multiple functions are executed on the same hook. Callback functions with smaller priority values are executed first.
php
function my_first_function() {
echo 'First!
}
add_action( 'wp_footer', 'my_first_function', 5 ); // priority 5
function my_second_function() {
echo 'Second!
}
add_action( 'wp_footer', 'my_second_function', 15 ); // priority 15
In this example, themy_first_function will be executed first because it has a lower priority.
2. parameters
Some hooks pass parameters to the callback function. You can pass parameters to the callback function via the accepted_args to specify the number of arguments the callback function accepts. Example:
function my_custom_comment_function( $comment_id ) {
function my_custom_comment_function( $comment_id ) {
// Perform the action associated with the comment
echo "The comment ID is: $comment_id"; }
}
add_action( 'comment_post', 'my_custom_comment_function', 10, 1 );
summarize
add_action is an extremely powerful feature in WordPress that allows developers to insert custom logic into the WordPress lifecycle. Whether it's adding a menu, registering a custom post type, or inserting code into a specific part of a page, theadd_action All can help you achieve a high degree of customization.
Through the mastery of add_action With the use of hooks, developers are able to create richer and more flexible features in WordPress. Hopefully this article will help you better understand and utilize the WordPress hook system.
Link to this article:https://www.361sale.com/en/50494The article is copyrighted and must be reproduced with attribution.





















![Emoji[wozuimei]-Photonflux.com | Professional WordPress repair service, worldwide, rapid response](https://www.361sale.com/wp-content/themes/zibll/img/smilies/wozuimei.gif)
![Emoticon[baoquan] - Photon Wave Network | Professional WordPress Repair Services, Worldwide Coverage, Rapid Response](https://www.361sale.com/wp-content/themes/zibll/img/smilies/baoquan.gif)

No comments