Making WordPress Filters Work for You

Sal Ferrarello / @salcode

( Jump to Resources )

My First Junior High School Dance

WordPress/PHP Functions

  • Something goes into them, argument(s)
  • Something comes out, the return value
Bugs Bunny's Carrot Machine Book Cover

Example Function

function fe_example( $str ) {
	return $str;

Phil Function

function fe_phil( $str ) {
	return "No, " . $str;

Phil Title Filter

add_filter( 'the_title', 'fe_phil' );

function fe_phil( $str ) {
	return "No, " . $str;
Two screenshots of my About page; one normal, one with 'No, ' prepended to the title
Diagram of get_the_title() function, which passes through the value unchanged
Diagram of get_the_title() function with fe_phil() filter, which passes through the value with 'No, ' prepended
Two screenshots of my About page; one normal, one with 'No, ' prepended to the title
Two screenshots of Recent Posts; one normal, one with 'No, ' prepended to the title
Backend screenshot showing with 'No, ' prepended to each title


Refrigerator Water Filter which is a good analogy for WordPress filters

Add Message to Login Screen

add_filter( 'login_message', 'fe_login_mg' );

function fe_login_mg( $orig ) {

Use your words wisely

' . $orig; }
WordPress Login Screen with message 'Use your words wisely'


add_filter( 'admin_footer_text', 'fe_aft' );

function fe_aft( $orig ) {
	return ''
		. 'This site created by Sal'
		. 'with WordPress';

Before Filter

WordPress Admin Footer with Default Message

With Filter

WordPress Admin Footer with my Custom Message

Where Do I Put This Code?

  • functions.php
  • a custom plugin you write
  • mu-plugins directory

mu-plugins ?





add_filter( 'the_content', 'fe_disclaimer' );

function fe_disclaimer( $content ) {
	return $content
	    . '
' . 'My personal opinion' . '
'; }

Disclaimer via Filter

An example notification added to the WordPress content with a filter

Close all comments

	'comments_open', '__return_false' );

Modifying RSS Feed

add_filter( 'the_content_feed', 'fe_rss_ad');

function fe_rss_ad( $content ) {
    $hire_blurb = "\n

Hire Sal

\n"; return $content . $hire_blurb; }


    'hook',          // what to filter
    'function_name', // a.k.a. callback



    'hook',          // what to filter
    'function_name', // a.k.a. callback
    10,              // priority (order)
    1                // # of args to pass in


Order in which the filters are applied.

add_filter( 'the_title', 'fe_colon', 10 );
add_filter( 'the_title', 'fe_paren', 10 );

"About Sal" becomes "About Sal :)"

add_filter( 'the_title', 'fe_colon', 10 );
add_filter( 'the_title', 'fe_paren', 5  );

"About Sal" becomes "About Sal ):"

Number of arguments

Pass the function two values

	'comments_open', 'fe_no_page_com', 10, 2);

function fe_no_page_com( $open, $post_id ) {

	$post_type = get_post_type( $post_id );

	if ( 'page' === $post_type ) {
		return false;
	return $open;

See comments_open documentation

WordPress Core Filters

Over 1,300 of them

Plugins often have filters too

How do I Find Filters?

Stop Emails Plugin

Was not stopping BuddyPress notifications

Force BuddyPress to use wp_mail()

See commit ad09c1d

Passwords from Magento

Migrating Magento Passwords to WordPress

How Passwords Work

"correct horse battery staple"
hash function outputs

Logging In

What you typed in gets goes through the hash function and compared to the stored value


Magento and WordPress use two different hashing functions

check_password filter

to the rescue

  'check_password', 'fe_mg_pwd', 10, 2 );

function fe_mg_pwd( $allowed, $plain_pwd ) {
	$hashed_pwd = magento_hash( $plain_pwd );

	if ( $hashed_pwd === $stored_mg_hash ) {
		return true;
	return $allowed;
  • Prepend "No, " post titles
  • Add message to login screen
  • Brand Admin Footer Text
  • Add disclaimer to all posts
  • Close all comments
  • Add advertisment to RSS feed posts
  • Close comments on pages only
  • Migrate Magento Accounts


Iron Code Studio

Sal Ferrarello



Image Credits

  • https://pixabay.com/en/feet-legs-shy-shyness-jeans-jean-723309/
  • http://looneytunes.wikia.com/wiki/Bugs_Bunny's_Carrot_Machine
  • https://commons.wikimedia.org/wiki/File:Rusty_pipes.jpg