Introduction to WordPress Hooks

WordPress hooks enable us to literally hook into parts of the WordPress page life-cycle to retrieve, insert, or modify data, or they allow us to take certain actions behind the scenes.

There are 2 types of WordPress Hooks. They are 1) Action Hooks 2) Filter Hooks

Action Hooks:

Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources (outside of the scope of where the hook is… either in the core, theme or plugin) to insert additional code and, there by, customize the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between the <head> and </head> tags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.

For Example:

	add_action( 'wp_head', 'custom_actionhook_example' );
	function custom_actionhook_example () {
	   echo '<meta name="description" content="This is the meta description for this page." />' . "";
	} // End custom_actionhook_example()

Filter Hooks:

Filter hooks are used to manipulate output. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off—whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).

Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

	add_filter( 'the_content', 'custom_filterhook_signoff' );
	function custom_filterhook_signoff ( $content ) {
		if ( is_single() ) {
			$content .= '<div class="sign-off">Th-th-th-th-th That\'s all, folks!</div>' . "";
		} // End IF Statement
		return $content;
	} // End custom_filterhook_signoff()

Where can I learn more about action and filter hooks?