What Is A WordPress Hook?

By

Posted On:

You can’t spend too long working in WordPress without finding out that you need a “hook.” Hooks are WordPress’ system for you to do something at a specific event. Hooks can be used to either change the value of something at some point — we call this a filter — or to do something, which is called an action.

A very basic example use case of a filter is to add a call to action message at the end of every post. We can do this with a filter called “the_content” which allows us to modify post content. Here is how we would do that:

add_filter( 'the_content', function( $content ) {
	$content .= 'Sign Up For My Awesome thing';
	return $content;
});

One of the most common uses for an action is to output inline JavaScript, such as Google Analytics tracking code in the head of our page. We use an action for this, specifically wp_head, since we’re not changing a value. Instead we’re using the action to “hook in” and output some new content.

Here is a basic example of how you can add your own JavaScript to the header of every page on your site:

add_action( 'wp_head', function( ) {
	echo "";
});

3 thoughts on “What Is A WordPress Hook?”

Leave a Reply

Your email address will not be published. Required fields are marked *