This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.

PMG Digital Made for Humans

The Most Interesting File in WordPress

2 MINUTE READ | May 25, 2012

The Most Interesting File in WordPress

The thing that makes WordPress an amazing flexible CMS (and, increasingly, a basic application framework) is its system of hooks.

Hooks are places where a developer can fire a function that either performs specific actions or filters/changes the output that gets sent to the user.

The WordPress core effectively eats it’s own dog food. Rather than specifically formatting the output of the_content() inside the function, for instance, the WordPress core adds filters to the_content. This means, of course, that you, as a user/developer are free to unhook those formatting functions and do whatever you want.

The Most Interesting File in WordPress

To see all the actions and filters the WordPress core uses, you needn’t look any further than wp-includes/default-filters.php.

As an example, let’s explore what exactly happens when one uses the the_content(); function. Here’s that function:

<?php

function the_content($more_link_text = null, $stripteaser = false) {  $content = get_the_content($more_link_text, $stripteaser);  $content = apply_filters('the_content', $content);  $content = str_replace(']]>', ']]>', $content);  echo $content;

}

Most of the WordPress core follows this sort of design pattern: the_something is a wrapper around get_the_something that only echoes out the value (and usually runs it through a filter).

In this case, the line we’re interested in is the apply_filters('the_content', $content);. This tells WordPress to fire the filter hook the_content. Any functions hooked into the_content will fire, receiving the post content as an argument. Now that we know which filter fires, we can go explore the default filters file to see what happens:

<?php

add_filter( 'the_content', 'wptexturize'        );

add_filter( 'the_content', 'convert_smilies'    );

add_filter( 'the_content', 'convert_chars'      );

add_filter( 'the_content', 'wpautop'            );

add_filter( 'the_content', 'shortcode_unautop'  );

add_filter( 'the_content', 'prepend_attachment' );

There are six separate functions hooked into the_content here – there are actually a few more hooked into the_content in other parts of the default filters file. Now that you know what functions are getting fired, you can go and figure out what exactly they do. wptexturize, for instance, converts certain characters into their html entity equivalents.

Why This Matters

Insights meet inbox

Sign up for weekly articles & resources.

Most of the time, how the core uses its own hook system doesn’t matter. But when you want to change WordPress’ default behavior, you are able to. It’s also possible that the filters the core uses may conflict or cause unexpected behavior in your theme or plugin. At that point, it’s up to you to track things down.


Posted by Christopher Davis

Related Content

thumbnail image

Get Informed

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

Get Informed

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

Get Informed

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

Get Informed

Trading Symfony’s Form Component for Data Transfer Objects

8 MINUTES READ | September 3, 2019

Get Inspired

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

Get Informed

Parsing Redshift Logs to Understand Data Usage

7 MINUTES READ | May 6, 2019

Get Inspired

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Get Informed

Testing React

13 MINUTES READ | March 12, 2019

Get Inspired

Tips for Designing & Testing Software Without a UX Specialist

4 MINUTES READ | March 6, 2019

Get Informed

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

BACK TO ALL POST