At the time of custom theme and plugin, creation or customization we need to use functions. Here Hooks are those functions we can apply to Actions and Filters in WordPress. By using these functions we can modify the functionality and presentation of theme and plugin and also we can change the default WordPress functionality. The main purpose of hooks to automatically a function that has the ability to modify or extend the functionality of a theme or plugin. Hooks allow us to easily add custom code within the WordPress themes and plugins. Here are the types of Hooks:
Actions:
Actions are used to add new functionality at a specific state in the processing of the page. Actions are the functions that we perform when a certain event occurs. For example, we need to add new widgets or menus or add a message to your page. It works like a trigger. Like, show a message when someone publishes a post. There are different types of Actions in WordPress related to different categories. To add an Action hook you just need to know in which hook you want to add your action. Now here is the code to add a hook into an Action.
add_action( $hook, $function, $priority, $args );
In the above syntax, the $hook and $function are the required parameters of the add_action function and others are optional parameters. $hook is the name of the hook in which you want to add your new changes. $function is the function that contains your additions code. The priority is an integer value that shows the priority of order for functions tied to that hook. The last parameter is used when we need to pass multiple arguments. Now here is the example to show how it works.
Register a Custom Menu in the Admin
function register_my_custom_menu()
{
add_menu_page( 'menu title', 'custom menu', 'manage_options','myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 );
}
add_action( 'admin_menu', 'register_my_custom_menu' );
In the example above we created a function register_my_custom_menu_page and hooked it into the admin_menu action hook. This allows us to run code when the admin menu is being generated.
Filters:
A Filter allows us to modify WordPress data before it’s sent to the database or the browser. In simple words, it is used to modify functionality and change stuff at a particular point or it allows us to modify certain functions. The filter hook is used to modify, filter, or replace a value with a new one. Here is the code to add a hook into a Filter.
add_filter( $hook, $function, $priority, $args );
The above syntax is looking the same as the add_action filter. It works the same way as add_action. The main difference is that when we work with a filter, we going to receive some data at the end of the function, then we have to return that data back. When we working with action, we are not receiving and modifying data, we were simply given a place in the WordPress runtime where we can execute our code. Now here is the example to show how it works.
Change the Excerpt Length
function custom_excerpt( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= wpb_continue_reading_link();
return $output;
}
add_filter( 'get_the_excerpt', 'custom_excerpt' );
In the above example, we created a function custom_excerpt which is hooked into the get_the_excerpt filter and applied the default WordPress continue reading link. As you can see we return the data here.
If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.