Changing the length of the_excerpt() in Wordpress

How can I change the length of the_excerpt() in Wordpress without editing core files?

Well, first we need to figure out exactly where the excerpt text is truncated. Checking wp-includes/default-filters.php, around line 128 we see:

add_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);

Ok, so that’s how it’s cut off. The first step in our fix is to prevent the default function from running and truncating the text at the default length. The easiest way to do this? Simply remove the filter by adding a line to your theme’s functions.php file:

remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);

Great. So now we’ve got the_excerpt() looking exactly like the_content(). Now we need to create our own pretty little function to handle truncating the_excerpt to the length we need. Since those crazy Wordpress devs have already thought of everything, we’ll use their original function as a template for our new one. The original function can be found in wp-includes/formatting.php, around line 779:

function wp_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = 55;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

See the line $excerpt_length = 55;? That’s the one we want to change!

Take your new wp_trim_excerpt function and stick it in your theme’s functions.php file. Be sure to rename it to something unique. You should end up with something similar to this:

function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = 75;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

Note that I only made two changes: 1) renamed the function to “custom_trim_excerpt”, and 2) changed the excerpt_length to 75.

Great, we’re almost there! One step left. Now we have to tell Wordpress to use our new custom function to truncate the_excerpt. We’ll do this by adding a new filter, similar to the one we removed at the very beginning. Stick this line in your theme’s functions.php file:

add_filter(‘get_the_excerpt’, ‘custom_trim_excerpt’);

Be sure to substitute your new function’s name, assuming you didn’t use the same one I did, and you should be all set!

Note: This guide was written using Wordpress 2.2. The exact procedure may vary with versions, but should be fairly similar.

Post Meta

6-8-2007
Date
7:58 am
Time
2521
Views
469
Words
Comments
Comment Feeds

Garland for Wordpress 2.2

Update 2/3/08: The links below are broken. There’s a new version of the Garland theme available here!

Attention all Garland-wanters! The Wordpress.com adaptation of the Garland theme is now available for stand-alone Wordpress 2.2 installations.

I know a lot of people have been disappointed after actually downloading the raw SVN-exported version from my previous post, and I finally took the time to check into what was wrong. Surprisingly, it didn’t really take long at all to get things working once I had a few minutes of solid thinking-time.

I have tested this theme and verified it to be fully working on my local test installation of Wordpress 2.2, YMMV. If you notice any oddities, feel free to let me know. I can’t promise I’ll actually fix them, but I’ll at least read them…

Changes

  • Fixed broken and missing Javascript includes that are required by the color-picker in the admin panel.
  • Corrected several URLs to images and stylesheets that had been hard-coded using relative paths so that they now correctly retrieve the stylesheet directory’s URI.
  • Added my lovely info to the credits. :)

Once again, I’ve tested these changes enough to release them, but I can’t guarantee it’ll work under every configuration. There’s really nothing terribly odd about this setup, but you never know…

Download
You can download the zip file here: garland-1.1.zip garland-1.1.2.zip

2.2 Only!
Please note that this theme requires the newly-released Wordpress 2.2, as it relies upon the jQuery Javascript library which was not included in prior versions.

If, however, you wish to place a copy of jquery.js in your wp-content/themes/garland/ directory, you should simply be able to un-comment line 39 in functions.php. I haven’t, however, tested this at all… Please let me know how it goes if you try.

Post Meta

5-16-2007
Date
9:27 am
Time
936
Views
292
Words
Comments
Comment Feeds

Worst. Organized. Project. Evar.

For the last two weeks, I’ve been working to design a one-off enrollment system1 for a client, and it seems like it’s just been one road-block after another.

First, their data was incorrect. And then again. And for good measure, a third time. To be fair, I don’t know if it was their fault or ours the second and third time (mis-communication somewhere about what translations we were making), but the first time certainly was2.

Then, there’s the fact that when I notified the person in charge of coordinating this data exchange with the client, they apparently “missed” the email… And so three days later I’m asking them where the hell my answers are, and they have no clue what I’m talking about.

Next, apparently no one really knows what data the client wants to get back from us for entry into their system. On some things, they claim to want all coverages. For others, just changes. Then there’s this one piece of information they don’t seem to have thought about nor want at all…

Finally, 4 days before the system is to go live and we’ll have thousands of people from across the country calling in to enroll using it, as I’m working my ass off to correct a few bugs and get final testing in, I notice that the spreadsheet I originally got says that the client is expecting data back for a coverage we’re not supposed to be enrolling them.

Uhh, what? How can we give them back data on something we’re not enrolling? Back to the project manager we go… “Oh, right… That spreadsheet is old, I’ll see if I can find a new version for you.”

WTF? 4 days from live and I’m the one that has to realize no one bothered to give the programmer the real specs? If these were freelance clients, I’d never work for them again… Alas, they’re internal company employees and I’m not their manager…

  1. We do health insurance claims-related stuff. []
  2. They duplicated first_name in the last_name field of the data file they sent us. []

Post Meta

5-7-2007
Date
11:47 am
Time
2559
Views
345
Words
Comments
Comment Feeds

Using CURL in XAMPP

There appear to be a lot of misguided people on the intarwebs claiming all sorts of varying things you have to do to get CURL to work on a Windows-based XAMPP install. I’d like to clear them all up here and now.

Using CURL in XAMPP

It’s really quite simple – uncomment extension=php_curl.dll in your php.ini file, then restart Apache1.

A lot of confusion seems to stem from the fact that there are multiple php.ini files in a standard XAMPP install. This really isn’t as confusing as you’d think it would be, assuming you – and I know I’m going out on a limb here – read the documentation.

There’s a straight-forward XAMPP FAQ Entry about their php.ini stucture. For those looking for the easy fix, ignore everything but the /apache/bin/php.ini file. That’s the only one that counts, regardless of which PHP version you’re currently using.

What about all the .dll files you need? Well, you don’t need them. Everything you need to run CURL on a XAMPP install is included in the download. Stop downloading extra crap and sticking it all over your system. XAMPP knows what it needs, where it’s at, and how to use it. End of story.

This is all based on the XAMPP Basic Package for Windows, version 1.6.1, released on 4/18/2007. YMMV.

Update for XAMPP 1.7.x: As of the 1.7.0 release, the php.ini file is found in /xampp/php. When in doubt, consult the FAQ entry linked above for help. Thanks to several commenters for pointing out this change!

  1. Restarting Apache is required on both Windows and Linux whenever you make configuration changes. []

Post Meta

4-21-2007
Date
12:04 pm
Time
5845
Views
276
Words
Comments
Comment Feeds

Javascript Documentation

At the end of the Yahoo! javascript videos I mentioned earlier, they include links to all the major documentation sources for browser Javascript implementations. For convenience, I thought I’d just link to all of them right here on a single page:

Mozilla

Here

Microsoft

Here

Apple

Here

Opera

Opera has no formal documentation on their DOM implementation. Instead, they simply link you to the W3C.

W3C

Here

Post Meta

3-4-2007
Date
11:19 am
Time
199
Views
69
Words
Comments
Comment Feeds