Difference Between Action and Filter Wordpress

Leave a Comment

Filter Hook Wordpress:

Filter hook in wordpress allow us change the data while printing data to user browser or while saving it to the database.

Following filter will remove read more text link when you  print the wordpress post the_excerpt().... This will change the Read More... link to ..... .

function new_excerpt_more( $more ) {
	return '.....';
}
add_filter('excerpt_more', 'new_excerpt_more');

Action Hook Wordpress:

Where as Action hook in the wordpress allow as do some stuff. For example if I want to add some css in the head section of the page following wp_head() action hook let us add the css to the head of the page.

add_action('wp_head','hook_css');
function hook_css()
{
	$output="";
	echo $output;
}
We simply define both action and filter this way i.e.
Filters change data while actions allow us to do stuff.

when should I use an action and when a filter?

You would want to use an action where you need to do something at a certain point in time... add options to the database or print css or javascript. You should use a filter where you would want to alter the value of some data that WordPress has passed through the apply_filters() function.

0 comments:

Post a comment