This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.
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.
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.
Stay in touch
Subscribe to our newsletter
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