WordPress Plugin: Shorten Link Text

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:

Screenshot 1

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

Screenshot 2

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!

Download and Installation

Download the plugin using this link:

shorten-linktext.zip

To install, simply:

  1. Unzip the file
  2. Upload “shorten-linktext” folder (and contents) to the “/wp-content/plugins/” directory
  3. Activate the plugin through the “Plugins” menu in WordPress
  4. Optional: visit the “Settings” menu and click “Shorten Link Text” if you want to customize the length, ellipsis text, or toggle it on and off without having to deactivate the whole plugin
  5. Ice cream sundae!

FAQ

Can I customize the length of the post titles?
Yep. It’s under Settings > Shorten Link Text.

Does it alter the titles of my posts?
Nope, just the display in that one place (usually found in the single.php template file).

Can I change the “…” ellipsis to something else?
Yep, under Settings > Shorten Link Text. You can use HTML if you want.

Can I see a screenshot of the settings page?
Meh, why not.

Screenshot 3

Under the Hood

Just in case you’re curious, here is an excerpt of the part of the plugin which does the actual processing:

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"

Leave a reply

The following XHTML tags are permitted: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">