Wordpress Action and Filter hook with example
Sumit Mishra
Php Architect || Technical Strategist || IT Consultant || Help You In Building IT Team || Project Outsourcing
In WordPress, action hooks and filter hooks are essential mechanisms that allow developers to customize and extend the functionality of WordPress themes, plugins, and core features. Here's an explanation of both types of hooks along with examples:
Action Hook:
Action hooks in WordPress allow you to execute custom functions or code at specific points during the execution of WordPress core, themes, or plugins. They are used to perform tasks without modifying the core code directly. Action hooks are often used for adding or modifying content, performing actions, or triggering events.
Example of Using an Action Hook:
Let's say you want to add a custom message to the WordPress login screen. You can use the login_message action hook for this:
// Define a custom function to display a message on the login //screen.
function custom_login_message() {
echo '<p>Welcome to our website. Please login to access your account.</p>';
}
// Hook the custom function to the 'login_message' action.
add_action('login_message', 'custom_login_message');
In this example, when the login_message action is triggered (which happens when the WordPress login screen is displayed), our custom_login_message function is executed, and it adds the custom message to the login screen.
Filter Hook:
Filter hooks in WordPress allow you to modify data or content before it's displayed or processed. Filters provide a way to change the output or behavior of WordPress core, themes, or plugins by altering values. Filter hooks are typically used for data manipulation, content modification, and enhancing functionality.
Example of Using a Filter Hook:
Let's say you want to modify the default "Read More" link text in WordPress excerpts. You can use the excerpt_more filter hook for this:
// Define a custom function to change the "Read More" link text.
function custom_excerpt_more($more) {
return '... <a href="' . get_permalink() . '">Continue reading</a>';
}
// Hook the custom function to the 'excerpt_more' filter.
add_filter('excerpt_more', 'custom_excerpt_more');
In this example, when WordPress generates an excerpt, the custom_excerpt_more function is called via the excerpt_more filter. It modifies the default "Read More" link text to "Continue reading" and returns the updated value.
By using action and filter hooks in WordPress, you can easily customize and extend WordPress functionality without modifying core files, making your code more maintainable and adaptable to future updates.
WordPress add_action with example
In WordPress, the add_action function is a fundamental concept used for hooking custom functions or methods to specific WordPress actions or events. This allows you to extend or modify WordPress core functionality, themes, or plugins by running your own code at specific points in the WordPress execution process.
Here's an example of how to use add_action:
领英推荐
// Define a custom function that will be executed when the //'wp_footer' action is triggered.
function my_custom_footer_code() {
echo '<p>This is my custom footer content.</p>';
}
// Add the custom function to the 'wp_footer' action.
add_action('wp_footer', 'my_custom_footer_code');
In this example:
1. We create a custom function my_custom_footer_code that contains the code we want to execute when the wp_footer action is triggered.
2. We use the add_action function to attach our custom function to the wp_footer action. The first parameter 'wp_footer' specifies the action hook we want to attach to, and the second parameter 'my_custom_footer_code' is the name of our custom function.
Now, whenever the wp_footer action is triggered (usually at the end of the HTML footer section in WordPress), our my_custom_footer_code function will be executed, and the content "This is my custom footer content." will be displayed in the website's footer.
You can use add_action to hook your custom functions to various other actions in WordPress, such as 'wp_head', 'init', 'save_post', and many more. This allows you to customize and extend WordPress functionality to suit your needs.
WordPress add_filter with example
In WordPress, the add_filter function is used to modify or filter the output of a specific WordPress filter hook. Filters allow you to manipulate data before it's displayed or processed by WordPress, themes, or plugins.
Here's the basic syntax of add_filter:
add_filter( $hook_name, $callback_function, $priority, $accepted_args );
add_filter( $hook_name, $callback_function, $priority, $accepted_args );
Here's an example of how to use add_filter in WordPress:
// Define a callback function to modify the content
function custom_modify_content( $content ) {
// Append text to the content
$modified_content = $content . '<p>This content has been modified.</p>';
return $modified_content;
}
// Hook the callback function to the 'the_content' filter
add_filter( 'the_content', 'custom_modify_content' );
In this example:
Now, when you display post content using the_content() in your WordPress theme, it will include the additional text from our callback function.
<?php
// Display post content with the modified content
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_content();
}
}
?>
This is just a basic example of how you can use add_filter to modify content in WordPress. Filters are used extensively throughout WordPress, themes, and plugins to customize and extend functionality.