WordPress CMS provides hooks that allow programmers to create, update and modify WordPress themes and Plugins with “Shortcode” without changing the original files written by original creator.

Types of hooks, there are two types :
1)Action Hooks
2)Filter Hooks

1)Action Hooks :

Action hooks are available in WordPress core as to where it’s possible for external resources to insert additional code.

Some examples : wp_head() , the_post(), get_sidebar() are an action hooks which are used in almost all of the themes, as a standard or best practices.

To hook an action, you can create a hook in your function file. You can hook it using add_action() function. Hope this clarifies action hook more.

Let’s take a look at code sample of Action hook :

add_action( ‘wp_head’, ‘head_func’ );

function head_func () {

echo “This is a hook test”;

}

2) Filter Hooks

Filter hooks can be used to handle output where you add text or content at end.

You can add a filter using add_filter() function. There are various filters used in WordPress.

Some examples are : the_title(), wp_title(), get_the_excerpt(), get_to_ping(), attachment_icon().

Sample Code : Using the filter below you can add content at end of the post.

add_filter( ‘the_content’, ‘head_func’ );

function head_func( $content ) {

if ( is_single() ) {

$content .= ‘This is a filter hook test’ . ” “;

}
return $content;
}

Advertisements