WordPress Hooks Actions and Filters is a subject that many people who are starting in the development of WordPress ask themselves regarding Hooks, which is a place where you can add your codes or modify what WordPress does by default. The hook is a very used resource for anyone who wants to develop their themes with functions and for those who also want to build plugins for WordPress.
I did a lot of research on this topic because what I found on the web explains, but not in a “friendly” way, to understand who is starting in the development. This article, published by Zac Gordon of the Treehouse team, was what I found most friendly, So I decided to write a post about it. I hope you enjoy the content.
Make your WordPress site’s Load Blazing Fast Just by moving to Nestify. Migrate your WooCommerce Store or WordPress Website NOW.
Hooks in WordPress allow developers to quickly tie up their code with core code themes and plugins. In this article, we’ll find out what Hooks are, and let’s move on from the different types of hooks and see some examples of hooks in action.
Definition of terms
A hook is a generic term in WordPress that refers to places where you can add your code, change what WordPress is doing, or give an output by default. There are two types of hooks in WordPress: Actions and Filters.
An Action in WordPress is a hook that is triggered at a particular time when WordPress is running and allows you to take action. This may include creating a Widget when WordPress initializes or sending a Tweet when someone posts a post.
A WordPress filter lets you get and modify data before sending it to the database or browser. Some examples of filters include customizing how snippets are displayed or adding some custom code to the end of a blog post.
At first, it can be a bit confusing to find out if something is an action or a filter. One crucial difference is that when you work with a filter, you will get a piece of data, and then at the end of your function, you have to return that data.
With an action, on the other hand, you’re not getting data modification. You’ll have a place in the WordPress runtime where you can run your code.
Types of Hooks: Actions and Filters
The WordPress Codex (official platform documentation) has two essential pages that can help you to find out which Hooks are available in WordPress.
The WordPress Action Reference page has provided actions listed in the following categories:
- Actions Run During a Typical Request
- Actions Run During an Admin Page Request
- Post, Page, Attachment, and Category Actions (Admin)
- Comment, Ping, and Trackback Actions
- Blogroll Actions
- Feed Actions
- Template Actions
- Administrative Actions
- Dashboard “Right Now” Widget Actions
Codex also has a similar Filter Reference page that lists the following categories:
- Post, Page, and Attachment (Upload) Filters
- Comment, Trackback, and Ping Filters
- Category and Term Filters
- Link Filters
- Date and Time Filters
- Author and User Filters
- Blogroll Filters
- Blog Information and Option Filters
- General Text Filters
- Administrative Filters
- Rich Text Editor Filters
- Template Filters
- Registration & Login Filters
- Redirect / Rewrite Filters
- WP_Query Filters
- Media Filters
- Advanced WordPress Filters
- Widgets
- Admin Bar
Many of these Hooks Filters are separated into subcategories: database Reading and database Writings. This depends on whether you are reading from the database before displaying it on a page or editing a screen or whether you are writing code before saving data in the database.
Working with Hooks in WordPress starts with figuring out what Hook you need to tie your code and then writing the code to modify the data you need or perform whatever action you need.
If you get stuck, or you’re not sure which Hook to use, you can usually figure it out by looking for something like: “. WordPress Actions for [whatever you want to link] “The WordPress Stackexchange (WordPress Stack Exchange Question and Answer Site) has a lot of results with questions like this as well.
How to add and remove your functions
The process is quite simple if you want to do your own job. First, you need to know some information. For Actions, you’ll want to see the name of HookHookwell as to when exactly it runs. For Filters, you also need to know the name of HookHookt you wish to see the value you will get and that you have to return, too. The final bit of information you need is the function’s name, where you have all of your code.
How to Convert a Hook to an Action
add_action( $hook, $function_to_add, $priority, $accepted_args );
The required parameters of the add_action function are the hookHook function to be added. The priority is an optional integer value based on a scale from 1 to 999 that determines the order priority for parts attached to that particular hookHookgher priority means that it runs later, and lower priority means earlier. The last parameter is used less frequently when you need to pass or accept multiple arguments.
How to convert a Hook into a Filter (Filter)
add_filter( $tag, $function_to_add, $priority, $accepted_args );
The add_filter works the same as add_action. You’ll have to be careful, too, because sometimes a hook exists as much as an action and a filter, or a filter and a function. You will see the real difference with the actual position you call.
Remember that for a filter, the function_to_add both gets a value and has to return it at the end of the function. On the other hand, the actions simply execute the code they need and do not return a value.
How to Remove a Hook from Actions and Filters
To remove a hook is quite simple. Use the remove_action or remove_filter function together with the hook name, process, and priority. Priority is optional and helpful if you have to disengage a position turned on more than once and only want to remove a specific instance of that function.
remove_action( $tag, $function_to_remove, $priority );
remove_filter( $tag, $function_to_remove, $priority );
Now that we have examined the basics of how functions are hooked and unhooked, let’s look at some real-world examples of some different Hooks in action.
Examples of WordPress Hooks in Action
There are over 200 hooks in WordPress. Below you will find some examples of standard hooks in use.
Register a Custom Menu in Admin
function register_my_custom_menu_page() { add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 ); }
add_action( 'admin_menu', 'register_my_custom_menu_page' );
In the above example, you can see the register_my_custom_menu_page function being connected to the admin_menu action hook. This allows you to run the code when generating the administration menu. This is most commonly used to add a custom menu link to a plugin or theme.
Changing the length of the roof
function excerpt_length_example( $words ) { return 15; }
add_filter( 'excerpt_length', 'excerpt_length_example' );
In this example, we use the excerpt_length filter, which gives us an integer that determines the length used with the_excerpt (). If you are unsure what value is passed to a filter, you can search the core code of WordPress for apply_filters (‘filter_name’ and take a closer look at what’s happening with this filter.
Hook in Posting a Post
function publish_post_tweet($post_ID) { global $post;
// Code to send a tweet with post info
} add_action('publish_post', 'publish_post_tweet');
In the pseudo example above, you can see that we are connecting in an action called “publish_post,” which runs when a post is published. You can use this to do something like send a tweet with information about the posted position.
The actual code for this is more complex than we have room to cover, but it is an excellent example of an action you can perform when a post is published.
Hook on Widget startup
function create_my_widget() { register_sidebar(array( 'name' => __( 'My Sidebar', 'mytheme' ), 'id' => 'my_sidebar', 'description' => __( 'The one and only', 'mytheme' ), )); }
add_action( 'widgets_init', 'create_my_widget' );
Creating a widget is a simple and joint action to add to a theme or plugin. When you do this, you must connect to the widget_init action. This hookHookows you to run your code when widgets are being generated within WordPress, so it is the perfect hookHookadd your devices simultaneously.
Hook in Front-end scripts and styles
function theme_styles() { wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.min.css' ); wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' ); wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '', true ); wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'bootstrap_js'), '', true ); } add_action( 'wp_enqueue_scripts', 'theme_styles' );
This is a commonly used hook and one that you learn early in WordPress development. It allows you to generate the URL stylesheets and JavaScript files on the front end of your theme. This is the preferred method of linking to CSS and JS, rather than hardcoding the links in your theme header.
This is a commonly used hook, and you learn early enough in WordPress development. It allows you to generate URL stylesheets and JavaScript files on the front end of your theme. This is the preferred method of binding to CSS and JS instead of hardcoding the links in your theme’s header.
This was an introductory article to WordPress hooks. I recommend starting discussions on the Codex platform in WordPress Codex to learn more.
I hope you have enjoyed it. Leave your comments.