WordPress Actions

A WordPress ‘action’ is a specific PHP function that can be executed in specific places throughout the system. Actions are handy for a variety of reasons, for example: they let you add or remove functionality from or into the system. Say you want to add a specific piece of code into the footer of a theme. One way to do this would be by writing a new function and hooking it to the existing wp_footer core action.

Here’s an actual example of adding Google Analytics tracking code to the bottom of every page. Here’s what you’d put into your functions.php file:

function google_analytics_code() {
   put your own specific google analytics code here
}

add_action( 'wp_footer', 'google_analytics_code' );

Quick explanation of what’s going on: first we’re saying we want to define a new ‘function’ called ‘google_analytics_code’ – the code for which we’re placing inside the brackets. Once the function is defined we can now use it with ‘add_action’, which we’re using to tell the system we want to add our new function, ‘google_analytics_code’, to the action ‘wp_footer’.

Return to Glossary