Hooks are way to add or modifying the functionality or visuality of the site without altering the main/core code.
There are 2 types of hooks,Actions and Filters.
>> Actions are hooks that WordPress core launches at specific points during the page cycle during which we can add our own functionalities.
Example : load a custom script file in the footer of the page.
function mytheme_enqueue_script() {
wp_enqueue_script( 'my-custom-js', 'custom.js', false );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_script' );
>>Filters are similar to Actions, in that they occur during the certain points of a page loading.
However they are used to intercept, manage and return data before a page is rendered of before data is saved in the database.
Example : adding custom excerpt after the main content of post.
function wpb_custom_excerpt( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= wpb_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt' );