• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Actions and Filters hooks in WordPress

Last Updated on May 13, 2018 Favorited: 0 times

WordPress hooks are the great thing to learn if you want to be a plugin or theme developer.

The basic functions of WordPress Plugin API help you to build a simple and lightweight plugin or theme and are not limited.

Genesis Framework offers various hooks and filters you will love to know about. The combination of WordPress hooks and Genesis hooks creates infinite possibilities and help you to create a creative child theme or plugin.

GeneratePress theme is another example that has various hooks and filters built by the Tom Usborne developed WPShowPost plugin.

Hooks? What are those?

A hook is a generic term in WordPress refers to a place where you want to do some stuff. The work can be of different type like

  • Adding a content
  • Removing a content
  • Repositioning a content

Here content can be anything, a code, a filed, a data, an image, a script and more. Hooks are a great way to control the output of a code snippet. These Hooks allow you to do two things

  1. Change default functionality within WordPress and
  2. Add your own functionality without modifying core files.

There are two types of hook: 1. Actions Functions and 2. Filter Functions

1. Actions Functions – Action Hooks (“do” hook) in WordPress trigged at specific time to take an action and allow you to insert custom code at various points (wherever the hook is run). There are two integral parts to action hooks:

do_action() – this is where the “hooked” function is run
add_action() – this attaches a function to a hook, which is defined by do_action()

2. Filter Functions – Filter Hooks (“customize” hook) helps to get and modify the data before it sent to the database or browser which allow you to manipulate and return a variable which it passes (for instance a product price, availability). There are four primary core functions that you will often use when working with filters:

add_filter() – used for adding a new custom filter
remove_filter() – used for removing an already registered filter
apply_filters() – runs the provided data through the specified filter
has_filter() – checks whether a specific filter has been registered

Basic Functions

1. an action hook

add_action( $hook, $function_to_add, $priority, $accepted_args );

Parameters 
$hook (required)
The name of the location where we want our custom functionality to be executed. Take a look at Genesis visual hook which gives you an idea of exact location to adding anything in your Genesis child theme.

$function_to_add (required)
The name of the function that we will hook to our tag. It can be a custom, a standard PHP function, or one existing in the WordPress core.

$priority (optional)
Priority tells WordPress when a function will be executed. The lower the number, the earlier it will do his work and functions with the same priority are executed in the order in which they were added to the action.
The Default value of priority remains 10.

$accepted_args (optional)
The number of arguments the function accepts. Default value: 1

Check for Existence of Action Hook

The has_action() conditional works just like the has_filter(); we can simply pass the name of the action we want to check for. Like this:

if (has_action('name_of_action_to_check_for')) {
    // action exists so execute it
    do_action('name_of_action_to_check_for');
} else {
    // action has not been registered
}

2. a filter hook

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Parameters
$tag (required)
The name of the filter to hook the $function_to_add callback to.

$function_to_add (required)
The callback to be run when the filter is applied.

$priority (optional)
Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
Default value 10.

$accepted_args (optional)
The number of arguments the function accepts. Default value: 1

Functions Usage

Actions are used to:

  1. Add a new work to the frontend or backend
  2. Modify and Re-add the work at different priority
  3. Do other works

Filters are used to:

  1. Override the default data of existing fields
  2. Adding additional custom fields and data into a field
  3. Manipulating options retrieved from the database

Using Hooks

How to Hook into an action

add_action( $hook, $function_to_add, $priority, $accepted_args );

How to Hook into a filter

add_filter( $tag, $function_to_add, $priority, $accepted_args );

How to Unhook from actions

remove_action( $tag, $function_to_remove, $priority );

How to Unhook from filters

remove_filter( $tag, $function_to_remove, $priority );

Points to Remember:

For a filter, the function_to_add receives a value and has to return it at the end of the function. Actions, on the other hand, simply execute the code they need to and don’t return a value.

Examples

To use a hook to add or manipulate the default code, we can add our custom code to your theme’s functions.php file.

Using action hooks

To execute a custom code, we need to hook in by using the action hook do_action(‘action_name’); . Here is the sample code:

add_action( 'action_name', 'some_function_name' );

function some_function_name() {
// Here to do code work
}

Using filter hooks

Filter hooks are called throughout are code using apply_filter( ‘filter_name’, $variable ); . To manipulate the passed variable, we can do something like the following:

add_filter( 'filter_name', 'some_function_name' );

function some_function_name( $variable ) {
// Here to do code work
return $variable;
}

Real Examples:

Let’s take some real-life examples you could see if you took interest in WordPress development.

Action Hooks

Example 1.
WordPress comes embed feature to load some contents from other sites. It is useful in some cases but i do not use it. This feature includes one additional request to the server for a js file called wp-embed.min.js. Here we can take an action to remove this.

add_action( 'wp_footer', 'custom_deregister_scripts' );
function custom_deregister_scripts(){
 wp_dequeue_script( 'wp-embed' );
}

Here, I have added a work (action) to do a function “custom_deregister_scripts” in wp_footer to dequeue (remove) the script named ‘wp-embed’ which is responsible to load that js.

Filter Hooks

Example 1.
WordPress includes emoji feature to show beautiful emojis on your site. This is unnecessary in most cases and creates additional request to get file wp-emoji-release.min.js. We can simply remove it with the help of action hook.

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

add_filter( 'emoji_svg_url', '__return_false' );

Here,
1st action removed the emoji script detection (js)
2nd action removed the emoji style (CSS)

and filter informed the site not to output emoji SVG URL (SVG icons)

I will update this article with more examples and illustrations soon till then check out the following resources:-

Reference & Sources:

  • Plugin API
  • Action Reference
  • Filter Reference

Resources:

Hooking into WordPress: What the Heck is a Hook?

  • WPseek.com
  • AdamBrown.info
  • WPfunction.me
  • WooCommerce Hooks

Additional:

  • Get started with hooks in WordPress
  • Learn WordPress hooks

Related Posts

  • WordPress, Genesis and Custom Post Types
  • Working with WordPress Customizer for Genesis
  • Conditional Tags and Return Early in Genesis
  • Genesis Hooks — Visual Hook Guide with Examples!
  • Genesis Hooks Reference: A Complete List of Available Hooks & Filters

Categories: Free Content, WordPress Tutorials Tags: hooks, WordPress, WordPress Hooks

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service