How to Use Add_action in WordPress


WordPress comes with many themes and plugins, but more importantly, it allows the user to modify and customize these themes and create plugins as per need. To use customization features, action hooks are used in WordPress and same is the purpose of this tutorial i.e. to learn how to use add action in wordpress.

Step # 1 – Using the hook

To add action in wordpress, we need to create a function to alter the CSS of our blog and insert that function via add action method.

Here in this tutorial we will change color of some of the links, so the following code is given here for reference and understanding.

function insert_some_css() {
echo CSS
style type=”text/css”
a {
color: #08FF00; /* green */
text-decoration: none;
}
a:hover {
color: #FF0000; /* red */
text-decoration: underline;
}
/style
CSS;
}

Once the function is added, it just needs to be associated to the action hook via the add_action method.

add_action(‘wp_head’, ‘insert_some_css’);

Using the hook

The image below gives the full picture of the code for wordpress add action