WordPress Hooks – Action and Filter hooks

WordPress hooks are, triggers that allow users to, modify areas like WordPress theme or plugin, or add some custom code to the various parts of WordPress without modifying original files.
In general Hooks are divided into Action and Filter hooks. Action hooks allow to modify wordpress data i.e. content of pages, post etc and Filter allow for manipulation of the content e.g. content of pages, post etc. Its a little bit confusing to choose the proper filter to be used.

The main difference is that when you work with a filter, you receive some piece of data and at the end of function you have to it return it back. On the other hand in action we are not doing anything with content/data, In Run-time you just get a place to execute your code.

WordPress- Hooks

Definition of Terms

A Hook is a generic term in WordPress that refers to places where you can add your own code or change what WordPress is doing or outputting by default. Two types of hooks exist in WordPress: actions and filters.

An Action in WordPress is a hook that is triggered at specific time when WordPress is running and lets you take an action. This can include things like creating a widget when WordPress is initializing or sending a Tweet when someone publishes a post.

A Filter in WordPress allows you get and modify WordPress data before it is sent to the database or the browser. Some examples of filters would include customizing how excerpts are displayed or adding some custom code to the end of a blog post.

At first, it may be a little confusing to figure out whether something is an action or a filter. The important difference is that when you work with a filter, you are going to receive some piece of data and then, at the end of your function, you have to return that data back. With an action, on the other hand, you are not receiving and modifying data, you are simply given a place in the WordPress runtime where you can execute your code.

Action Hook

For action hook, create a function in theme’s functions.php file and hook it using the add_action() function, as follows:

<?php
add_action( 'wp_head', 'your_function_name' );
function your_function_name () {
// Add your code here.
echo "My Text Here";
}
?>

This code will be get executed on WP_head hook and place its text in between tags.

Filter Hook

Filter Hooks are used to manipulate the content. An example of this hook is like add some Line at the end of the content for each post.
Example:

<?php
add_filter( 'the_content', 'Add_text_at_end' );
function Add_text_at_end ( $content ) {
if ( is_single() ) {
$content .= '<div>This should be at the end of content.</div>";
}
return $content;
}
?>

For more infromation take a look at following links:
Action hooks: http://codex.wordpress.org/Plugin_API#Actions
Filter hooks: http://codex.wordpress.org/Plugin_API#Filters

Do you need to develop a best website for your business? Just relax, Solace is there to help you. Develop the best website with our exert’s team which will be the face of your business. They believe in effectiveness of using wordpress with it’s new features for web development. Contact us for web development that enhance your company’s profitability.