Action Hooks and Filters 101

Sprinkled all throughout WP Framework are two type of functions called action hooks and filters. Those are the key ingredients that make WP Framework so flexible and customizable. In this guide, you’ll learn all about action hooks and filters and how you can take advantage of them for use in your themes.

Action Hooks

Action hooks are placeholder functions located all throughout WP Framework. They allow you to run your own functions at those particular spots you see them at. This allows you to do stuff at particular locations of the framework without editing the original function.

So whenever you see something similar to this:

<?php do_action( 'framework_hook_name' ); ?>

You can “hook” into that location by calling the add_action() function:

<?php add_action( 'framework_hook_name', 'my_custom_function' ); ?>

Where the first parameter is the name of the action hook your hooking into, and the second parameter is the name of the function you created to execute at that particular location. Here’s an example function that outputs ‘Hello World’ in that location:

function my_custom_function() {
	echo 'Hello World';
}

Filters

Filters are literally what they say they are, filters. Filters allow you to filter through data and modify it to your likings. This allows you to edit data you normally couldn’t edit without modifying the original function.

So whenever you see something similar to this:

$bool = apply_filters( 'framework_filter', (bool) true );

You can filter into that location and change it’s value by calling the add_filter() function:

add_filter( 'framework_filter', 'my_custom_filter' );

The first parameter is the name of the filter your modifing, and the second parameter is the name of the function that you created to modify that data. Here’s an example function that changes the value of $bool:

function my_custom_filter( $data ) {
	if ( $data ) {
		$data = false;
	}
	return $data;
}

Notice that we added a parameter to my_custom_filter() labeled $data. This is necessary as add_filter() passes the variable $bool into your function. Modify the data accordingly and return the results (instead of using echo).

To read more about action hooks and filters, see the official documentation in the Codex.

Download WP Framework

Discuss WP Framework

Stop by the forums and get help with your customizations!

    It Seem There Is Something Wrong With BBLD Configuration, Please Check It.

WP Framework on Twitter!

Follow WP Framework and stay in the loop on WP Framework happenings!