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:
- 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. - Then our function is taking the value passed to it by the
'the_title'
filter, that’s the whole next line. - Finally, we added the word “Hooked: ” to the front of the passed value
$title
and pass it back with thereturn
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
Happy hacking!
Master Modern WordPress Development 🏆
Get ‘Modern WordPress Fast Track’ today
Become a WordPress Developer Who Can Build Anything
Learn to build high-value block themes and plugins with AI and automation in a focused 10-week course. By the end, you’ll master block themes and plugins, leverage AI assistance, implement automated testing, optimize for scale, and confidently deploy professional WordPress solutions.
Presale is currently open with 40% OFF until May 23rd, 2025. 🚀
Course starts June 1st.
We have prepared special bonuses for everyone who enrolls during this period! 🎁
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.
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.
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?
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?
Good question! Something like this: