Answers

Question and Answer:

  Home  WordPress Theme Development

⟩ Explain what are the types of hooks in WordPress and mention their functions?

There are two types of hooks

1) Action hooks

2) Filter hooks

Action Hooks :- Action hooks are points in wordpress core where its possible for outside resources to insert additional code.

For example- wp_head() , the_post(), get_sidebar() is an action hook which is used by most of themes. To hook an action, create an hook in your function file and hook it using add_action() function.

<?php

add_action( 'wp_head', 'head_func' );

function head_func () {

echo "<div>This is test</div>";

}

?>

Filter Hooks :- Filter hooks are used to handle output like using it you will add an text or content at end of content of your post. You will add an filter using add_filter() function. There are various filter used in wordpress as the_title(), wp_title(), get_the_excerpt(), get_to_ping(), attachment_icon().

For example:– Using these filter we will add content add end of posts.

<?php add_filter( 'the_content', 'webs_expert' );

function head_func( $content ) {

if ( is_single() ) {

$content .= '<div>This is test</div>' . " ";

}

return $content;

}

?>

 239 views

More Questions for you: