Prepend and Append Text to WordPress Titles

By  on  

I was thinking about little ways to improve my blog and one small enhancement I wanted to make was adding more information to post titles when they show up in RSS feeds.  Something about notifying the user via title outside of the site itself seemed like a good idea.  For example, some posts are just small "quick tips", and more readers may take the time to peruse it quickly because they know it won't take too long.  Here's how I accomplished that feat with WordPress.

The PHP

The first step is creating a function, which is provided the post title and ID, that does a category check and prepends text accordingly:

function show_as_tip_in_feed($title, $id) {
	if(is_feed() && in_category('tips', $id)) {
		$title = 'Quick Tip: '.$title;
	}
	return $title;
}

Once the function is there, the add_filter hook enables the original function:

if(function_exists('add_filter')) {
	add_filter('the_title', 'show_as_tip_in_feed', 10, 2);
}

Of course you don't need to do the category check or RSS feed check -- you can append or prepend text to any and every title, and much like other WordPress functionalities, the API easily allows developers to do so.  Win!

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Comment Preview Using MooTools

    Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools. The XHTML You can set up your XHTML any way you'd like.

  • By
    Input Incrementer and Decrementer with MooTools

    Chris Coyier's CSS-Tricks blog is everything mine isn't. Chris' blog is rock star popular, mine is not. Chris prefers jQuery, I prefer MooTools. Chris does posts with practical solutions, I do posts about stupid video-game like effects. If I...

Discussion

  1. Im trying to automatically prepend and append a particular text to all post titles automatically. Your example above is not doing the trick. I dont want to do the rss and category check. I want it on all post titles. Please help.

  2. Chris

    Thanks – you showed the code for prepending, but not appending?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!