• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero Blog

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • VPS Hosting
    • WordPress Hosting
    • WooCommerce Hosting
  • Reseller
  • Enterprise
  • Domains
  • Account
  • Blog Home
  • Categories

How Do WordPress Actions And Filters Work?

Bhagwad Park

Published on: September 16, 2019

Categories: WordPress 0

We spend so much time on NameHero giving advice on which code to add to WordPress. It occurs to me that we haven’t given an explanation so far, about the most common mechanism in WordPress for modifications – actions and filters. So this is a quick introduction to them, why they’re useful, and two examples to give an idea of how they work.

What are Actions and Filters?

WordPress actions and filters allow us to pinpoint specific places where we want to perform certain actions. Using them, we can change the default output of certain functions – like a URL, or a date – and create our own ones instead. Or we can interrupt WordPress at specific times of a page’s creation, and make it do stuff like output HTML.

As of this writing, there are 2,247 hooks in WordPress. That’s a lot of customization! Using the right actions and filters, we can achieve just about any result we desire.

Why Not Just Change the Theme Files?

Let’s say you want to insert the “Last Modified Date” at the top of each blog post. The standard way to do this via a simple filter that prepends the date to the content. But why not just modify the theme file instead? Surely it’s much easier to just find the file that prints the content and add the date there?

While you can certainly do this, it turns out there are a few potential problems:

1. Updates to your Theme will Wipe out Your Changes

The most obvious reason not to modify theme files is that any theme update will overwrite your changes and restore the theme to its original conditions. In fact, it’s a good idea to leave your theme files alone as much as possible.

But wait! What if you have a child theme? Indeed, if you have to modify theme files, the best option is to create a child theme and make your changes there. This way when the theme updates, your changes are still safe. But that’s not all.

2. Switching your Theme Destroys All Your Work

Even if you’ve done the smart thing and created a child theme, your changes will be lost if you switch themes. Something like adding the modified date to a post should be standard functionality regardless of which theme is active. So it makes no sense to modify theme files.

3. You Lose Track of Your Changes

Finally, if you’ve made a bunch of changes to your theme files, you have no way of seeing all your changes in one place. You don’t know which modifications you’ve made, and which ones are the original. In addition, your changes are all mixed up with the code, and there’s no clear separation of presentation and function.

For these reasons, it’s far better to use filters and actions.

Example of an Action

The most common use of an “action” is to add some code to a specific part of the WordPress page – like say in the “header” section. Typically, we use this to insert something like Google Analytics code in the header. Here, the action is “wp_head”.

Example of a Filter

A basic use of a filter, is to print a link to the latest post. Instead of manually going into the theme files and making changes there, we can simply hook into the “the_content” filter and add the date there.

These two examples are a good showcase of how to use actions and filters in WordPress. It makes the platform infinitely extensible, and enables the tens of thousands of plugins that make WordPress so attractive.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!

Most Popular Posts

Speed up your site: solving the WordPress slowdown

NameHero’s Recommended WordPress Plugin and Theme Setup (2023)

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Domains
Help & Support
  • NameHero Blog
  • Support
  • Knowledgebase
  • Announcements
  • Affiliates
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2023 NameHero, LLC. All rights reserved.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA