WordPress Plugin: Shorten Link Text

November 18, 2008 by Traveling Matt

This is my first WordPress plugin. It doesn’t do much, but I hope it will come in handy for a lot of people. It takes the “Next Post” and “Previous Post” links at the top of single-post pages and shortens them so they fit in the space alloted for them by the template.

When you are viewing a single post (by clicking on its title on the main blog page, or the “Read More” link) these navigation links are typically found up above the post and allow the visitor to navigate quickly to the previous post or next post chronologically. But when those posts have titles which are particularly long, they can break the layout. An example from a blog I designed:

This plugin fixes that problem, shortening text and adding “…” (or whatever you specify) so that you end up with this:

For a summary of what this problem is and why it’s hard to fix, visit this WordPress support thread. It turns out these links are hard-coded in WordPress and without using some complicated output buffering and regular expressions, it’s difficult to shorten them so they fit in the box without breaking the HTML code and the <a> tag.

I wrote this to solve the problem. It will shorten the displayed titles of the next and previous blog posts, while maintaining the link HTML and without requiring any changes to your template. It does this by adding a regular-expression search to find and then shorten the title, and apply the change via the “apply_filter” WordPress hook, and invoking it for the “previous_post_link” and “next_post_link” filters. (More on that farther down.)

This was written and tested for WordPress version 2.6.2. I do not know whether it works on any other version. Feel free to try it and post your results in the comments!

// This adds filters to the next and previous links, using the above functions

// to shorten the text displayed in the post-navigation bar. The last 2 arguments

// are necessary; the last one is the crucial one. Saying "2" means the function

// "filter_shorten_linktext()" takes 2 arguments. If you don't say so here, the

// hook won't pass them when it's called and you'll get a PHP error.

add_filter('previous_post_link','filter_shorten_linktext',10,2);

add_filter('next_post_link','filter_shorten_linktext',10,2);

Support

Now the bad news: I am waayyyyy too busy to actively develop and support this plugin. It’s just a tiny little plugin, so this shouldn’t be a big deal. Nonetheless, I have to apologize in advance because I’m not a good enough coder to whiz through improvements and bugfixes as fast as the real heavy-duty plugin developers. I had to read through about twenty Codex pages to figure out how certain functions worked and basically muddle through as best I could.

So if you need help or have suggestions, post them here but understand that I may never have time to respond. I may never have time to change the code or update to new versions of WordPress. Just putting this package together is taking me several days! So I suck at it.

So, in conclusion, enjoy! If you get it working in your blog, don’t forget to have an ice cream sundae.

4 Responses to "WordPress Plugin: Shorten Link Text"

By Mark May 21, 2009 - 9:42 am

Hey, this is fantastic! I know you are not supporting this but I’ll bet you know the answer to my question off the top of your head. Would you be able to tell me what change I can make to the plugin to strip out any “” characters? Sometimes we place that in a headline when we want to force where a line breaks.

Thanks in advance.

By Mark May 21, 2009 - 9:43 am

Well, my code didn’t get in. It was the line break code. It do it with parenthesis instead of brackets: (br \)

By Mark May 21, 2009 - 10:49 am

Hmmm..I tried using the strip_tags function and it doesn’t strip anything (at least not Underlines and Linebreaks, which are what I need to strip):

I didn’t know anybody used BR tags in headlines! Anyway, it looks like you’ve misread the usage of “strip_tags”–plus you applied it to the $characters variable, which simply says how many characters to cut it to. How about trying this?