Beginner's Guide for WordPress / Start your WordPress Blog in minutes

What is: Action

In WordPress, an action is a bit of PHP code that developers can use to modify how a WordPress site works or add new features. WordPress has many predefined actions that allow developers to add their own code at specific points throughout the WordPress core.

Actions are one of the big features that makes WordPress so customizable.

What is Action

Throughout the WordPress code, there are “hooks” where developers can “hang” their own code. There are two types of hooks: action hooks and filter hooks.

Action hooks are where developers can place actions.

The action and filter hooks are the foundation of how the WordPress core, themes, and plugins work.

Note: If you’re a beginner, then we strongly caution against editing any WordPress files. Only experienced users who feel comfortable with editing functions.php file and have some knowledge of PHP should try this. Beginners should either use a plugin to accomplish the task they want to perform or consult professionals to edit code the code for you.

Before editing any code on your WordPress site we recommend that you backup your website in the event of a coding error. If you don’t have a backup plugin, be sure to read our article where we compare the best WordPress backup plugins.

What Are Action Hooks in WordPress?

Action hooks allow you to completely customize WordPress in any way you can imagine. Many themes and plugins use action hooks to add features.

For example, you can use an action hook to send an email to the author after a post is published, load a custom script in the footer of the page, or give your readers instructions on how to complete a form.

To get a better understanding of action in WordPress, it’s helpful to see how an action relates to two other terms, hooks and filters. Understanding these three terms together will make the term “action” a little easier to grasp.

Hooks are simply a position and do nothing by themselves. They are a place that allows developers to “hook in” their custom code into WordPress at specific locations and change how WordPress operates without editing core files.

Filters are codes that allow you to modify existing data before sending it back to WordPress to be processed before it displays the changed data.

A filter lets you do things like changing the length of an excerpt, placing related posts beneath your main content, or changing a price in WooCommerce.

Actions are also pieces of code that can create extra functionality at a specific point during a page’s processing. For example, you might want to add another widget or place a promotional message to your page.

Both actions and filters can be used in plugins or themes.

How to Add Action Hooks in WordPress

All Action hooks are added using the add_action() function. WordPress has several functions that allow you to use actions, but these are the ones that are most commonly used:

  • add_action(): this attaches a function to a hook you specified in the “do_action.”
  • remove_action(): this removes a function attached to a specified action hook.
  • do_action(): this is where the “hooked” functions will be run.
  • has_action(): checks to see if an action has been registered.

To give you an example of how they can be used, let’s assume that you want to add a copyright notice to your footer.

In order to do this you could modify your footer template directly. But many times it’s much easier, and better practice, to hook your code to a predefined action that’s already being executed in the footer.

To do this, you can add your copyright code into a function in your functions.php file. You can then add this function to an action that’s in the spot where you would like your copyright code to be executed.

function copyright_notice() {
   echo "Copyright All Rights Reserved";
}
add_action('wp_footer','copyright_notice');

In this example, copyright_notice is an action hooked into wp_footer hook. The function copyright_notice will be executed whenever the wp_footer() hook appears in a WordPress theme code.

Actions in WordPress provide a way for you to output content nearly anywhere in your theme. Likely, there is already a default hook that will do exactly what you need. WordPress offers a list of actions in their Plugin API/Action Reference.

We hope this article helped you learn more about actions in WordPress. You may also want to see our Additional Reading list below for related articles on useful WordPress tips, tricks, and ideas.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Additional Reading

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!