In WordPress development, an action can be used 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 make WordPress so customizable. They are pieces of PHP code that are ‘hooked’ to a WordPress event. When the event is triggered, the action will be performed.
They are used extensively by plugin and theme developers. However, if you’re not a developer, then you can paste code snippets from the web to add new features to your website. Many of these include actions.
What Is an Action in WordPress?
An action will modify the default behavior of a specific function. It does this by taking some information from WordPress and then doing something with it.
For example, actions can be used to place a promotional message on a page, activate a plugin, add extra widgets to a sidebar, publish a post, or add a menu to a header. They could also send an email to the author after a post is published, load a custom script in the footer, or give your readers instructions on how to complete a form.
Actions 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()
: attaches a function to a hook you specified in thedo_action
remove_action()
: removes a function attached to a specified action hookdo_action()
: this is where the “hooked” functions will be runhas_action()
: checks to see if an action has been registered
Actions are used by plugin and theme developers to add extra functionality to WordPress. You can also use actions to customize your theme by adding code snippets from online tutorials.
Only experienced users who feel comfortable with editing the 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 the code for them.
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, then be sure to read our article where we compare the best WordPress backup plugins.
We also recommend that you don’t add the code directly to the theme files, or you will lose your customizations the next time you update the theme.
Instead, you should use a code snippets plugin to add custom code in WordPress, create a site-specific plugin, or create a child theme and modify it instead of the parent theme.
See our guide on how to update a WordPress theme without losing customization.
What’s the Difference Between Hooks, Actions, and Filters?
To get a better understanding of actions in WordPress, it’s helpful to see how they relate to two other terms, hooks and filters. Understanding these three terms together will make the term ‘action’ a little easier to grasp.
Hooks are the foundation of WordPress plugin and theme development. They are places where developers can ‘hook’ their custom code into WordPress at specific locations and change how WordPress operates without editing core files.
There are two types of hooks.
- Action hooks allow you to do something. They let you add extra functionality and are executed when events like when a theme or plugin is activated, or when a post is published. Once the action has been performed, they don’t need to pass any information back to WordPress.
- Filter hooks allow you to change something. They intercept data that is being processed and let you modify it, then pass it back. They are used to filter output when it is sent to either a database or a user’s browser.
These action and filter hooks are the foundation of how the WordPress core, themes, and plugins work. They work together to allow developers great flexibility to modify default WordPress events, filters, and actions.
Developers can also create their own custom actions and filters so that other developers can extend their plugins or themes.
Examples of WordPress Filters
What does an action look like? Here are a few examples.
Let’s say that you want to add a copyright notice to your footer. To do this, you could modify your footer template directly. But it’s often 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 to 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 the code of a WordPress theme.
You can learn more by studying our WordPress guides that use actions. For example, here are a few guides that show you how to achieve something practical using WordPress actions:
- How to create custom navigation menus in WordPress themes
- How to add WordPress widgets to your website header
- How to add custom fields automatically on post publish
- How to add custom admin notices in WordPress
- How to disable the admin bar for all users except administrators
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 guide, then please consider subscribing to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.