KB Plugins

The best Wordpress plugins are free

Print this post

Truncate Titles

Let’s say you’ve written lots of posts with lengthy titles. But now you’re switching to a theme that doesn’t handle long titles well. Copy this into a file, name it truncateTitle.php (or whatever.php), upload to your plugins directory, and activate it in the usual way. Don’t leave any blank lines or spaces before or after this code (or you’ll get an error).

<?php
/*
Plugin Name: KB Truncate Title
Plugin URI: http://adambrown.info/b/widgets/category/custom/
Description: Truncates titles so they don't break your theme
Version: 1.0
Author: Adam R. Brown
Author URI: http://adambrown.info/
*/

define('KB_TRUNCATE_TITLE', 50); // change 50 to max number of characters to allow

function kb_truncateTitle($title){
	if (!is_int( KB_TRUNCATE_TITLE ) || 1>KB_TRUNCATE_TITLE )
		return $title;
	// rather than just use substr, let's make sure we cut it at the end of a word
	$arr = explode(' ', $title);
	$out = '';
	$count = 0;
	foreach( $arr as $str ){
		$count += ( strlen($str) + 1); // +1 is for the space we removed
		if ($count > KB_TRUNCATE_TITLE)
			break;
		$out .= $str . ' ';
	}
	// make sure we got SOMEthing
	if (!$out)
		$out = $arr[0];
	return $out;
}

// makes the function work
add_filter('the_title','kb_truncateTitle');
?>

6 comments »

1
Berklie opines
at 1:06 pm on February 4, 2008 #

I’m wondering what WordPress “titles” your script actually alters. For example, I’m looking for a script that only alters the top WordPress navigation titles of each post, as seen here:

Let Loose Upon Lollapalooza, Day 1
[link]

In the above example page, I’d like to take the navigation title “Let Loose Upon Lollapalooza, Day 3″ (top, right) and make it (and all titles longer than 20 char’s) get cut-off at 20 char’s with “…” placed afterwards, like so (without the quotes, those are for descriptive purposes):

“Let Loose Upon Lolla…”

Is this what your script does… or does it also truncate titles other than in the navigation?

2
Thus saith Adam
at 1:26 pm on February 4, 2008 #

Ahh, that’s what you’re asking. I read your earlier email too quickly. The best answer to your question is “try it and see,” since things don’t always work as intended. But it’s not intended to work the way you’re asking–it’s intended to truncate the titles when they occur at the beginning of each post on your blog. Note this line at the end of the code:

add_filter('the_title','kb_truncateTitle');

That’s what triggers the plugin. When your blog’s theme calls the_title(), the the_title hook gets activated, and my code runs. I don’t know whether that hook also gets called by the navigation links, but it does get called within the loop.

3
Berklie comments
at 9:11 pm on February 5, 2008 #

Hey there,
I’ll give it a shot when I’m back from traveling at the end of the month.

Thanks!

4
Sean opines
at 10:59 am on February 19, 2008 #

Hi,

Is there anyway to get this plugin to only truncate the titles displayed in the sidebars?

Example:

how do I trun…(L sidebar) How do I truncate titles? (actual post)

5
Thus saith Adam
at 11:05 am on February 19, 2008 #

Sean, that depends on how the titles are showing up in your sidebar. You can make the truncate function apply to anything you want, if there’s a filter hook for it. See my comment above about how the plugin uses the the_title hook.

You can find a complete list of WP hooks here: WP hooks database. Maybe you can find one there that will work for you.

6
Sean remarks
at 1:10 pm on February 19, 2008 #

Hi Adam,

THX! but darn it! Almost blown a fuse trying to get it to work the way I want - suffered even more frustration looking at the tables - oh my! The the_title hook truncates all post titles in both sidebars and main page. I just want it to truncate titles in recent posts (which rests in the sidebar) - to stop the text wrapping and spoiling my lovely rollover menu). Guess I’ll take a break from it before I accidentally put my fist through the monitor - heheh

Cheers anyway.

Leave a comment

Comment Guidelines
  • Yes, your comments will be visible to everybody. (Unless you use the private contact form.)
  • Allowed HTML: <a> <b> <blockquote> <cite> <code> <em> <i> <strong>
  • Code: Put code in `backticks` (above your "Tab" key) or it won't display well
  • Gravatars: To override the default image by your comment, use a gravatar
  • Links: If you include more than one link, your comment will go into the spam queue

Please read before commenting: Because my real job has me swamped at the moment, I am not supporting my plugins at this time. Sorry. This may last several weeks.

If you have a bug report, feature request, or other general feedback about a plugin, please leave a comment—but do not expect an immediate response. If you are requesting help, though, please check the plugin's documentation thoroughly rather than ask your question as a comment.

Thank you for your understanding.