Changing How Your WordPress Post Titles Appear: A First Filter Hook Tutorial

wordpress how to filter post title

This Quick Guide covers how to use code to change your WordPress post titles. This is separate from manually changing one or more WordPress post titles, which you can do without code. Instead, it’s the kind of thing you’d want to use to change something about a lot of post titles at once, like adding “(Sale!)” in front of all 200 products in a product category.

This Quick Guide is also an intro to writing your first WordPress filter function, since filters are the best way to change post titles. The filter hook we’ll be hooking onto is called, helpfully, the_title.

Here’s the full video guide to filtering your WordPress post titles:

And here’s a text guide to the same information. In the video above, we’ve changed all post titles on the site to include the word “Hooked: ” This is in the name of making the simplest WordPress filter function example we can. Here’s how we do that in PHP code:

add_filter('the_title', 'wpshout_filter_example');
function wpshout_filter_example($title) {
	return 'Hooked: '.$title;
}

How the Code Above Changes Your Post Titles

For a fuller explanation of the code above, here’s how it works step-by-step:

  1. We use the add_filter function provided to WordPress that when it “applies” the 'the_title' filter we want our function to be called. That’s the second thing we’re giving it: the name of our function.
  2. Then our function is taking the value passed to it by the 'the_title' filter, that’s the whole next line.
  3. Finally, we added the word “Hooked: ” to the front of the passed value $title and pass it back with the return keyword.

The code we don’t need to write is “what WordPress does with this”: WordPress handles that. Basically, our code takes in each post’s title, modifies it, and hands it back to WordPress to move ahead with. WordPress uses our modified version in the place of the title, and we don’t have to write any more than these four lines. It’s a really powerful system!

There’s a great deal more to know and understand hooks and filters in WordPress well, and our article on them is a great overview of the core distinctions and important details.

Here are some other resources you may love as well:

Learn WordPress Development: The Basic Course

The Best Way to Learn WordPress Development

Get Up and Running Today

Up and Running is our complete “learn WordPress development” course. Now in its updated and expanded Third Edition, it’s helped hundreds of happy buyers learn WordPress development the fast, smart, and thorough way.

Here’s what they have to say:

“I think anyone interested in learning WordPress development NEEDS this course. Watching the videos was like a bunch of lights being turned on.” -Jason, WordPress developer

“Other courses I’ve tried nearly always lack clear explanations for why WordPress does things a certain way, or how things work together. Up and Running does all of this, and everything is explained clearly and in easy-to-understand language.” -Caroline, WordPress freelancer

Happy hacking!


5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Shaharyar
June 11, 2021 2:41 pm

What If I want to add auto prefixes when someone creates a new post?
For example: I want to add “I will ” (just like fiverr) prefix automatically in the title when someone creates a new post.

Bob
March 3, 2021 8:40 pm

In newer versions of wordpress the_title hooks into the title. But it also hooks into nav menu item links.

As a result, functions that alter the title of the page will also alter every navigation menu link.

Alison
July 20, 2019 12:28 pm

Thanks for this nice clear tutorial. As I understand it, this filter only changes the way the title is displayed, but it does NOT change the actual post_title in the database. What if I want to write a function that DOES change the value in the database? I migrated a huge amount of content from a non-Wordpress site, and although I did a lot of cleanup in advance, there’s still much to be done. I’d like to be able to do some of that cleanup via WP functions if possible, rather than running queries directly on the database. For instance, many posts have their custom excerpts wrapped in tags. I’d like to remove those tags so I can have consistent padding/margins around excerpts. Suggestions?

Timothy Burgin
June 15, 2019 7:41 am

Cool. I would want to use this to change titles if the title did not contain a specific word…what would that code look like?

Fred Meyer
June 15, 2019 12:29 pm
Reply to  Timothy Burgin

Good question! Something like this:

add_filter( 'the_title', 'wpshout_change_title_if_no_sandwiches' );
function wpshout_change_title_if_no_sandwiches( $title ) {
	// If word's in there do nothing
	if( strpos( $title, 'sandwiches' ) !== false ) {
		return $title;
	}

	return '(No sandwiches in this title for some reason) ' . $title; 
}