Plugin: Better Excerpt
The problem with the_excerpt() is it's not very flexible. It's fixed at 55 words. It's wrapped in <p> tags. The ellipsis at the end is the ugly [...]. And without hacking WP core code, there's no way to change that.
A few people have come up with solutions to this. I like mine best.
- It modifies the standard the_excerpt(), and
- has an options page in WordPress admin, so
- there's no need to edit themes or learn PHP on the fly.
- You can change the length of the excerpt,
- the text and/or code that goes before and after it (wrap it in <div> tags or whatever you like)
- change the ellipsis or miss it out altogether, and
- optionally add an automatic link to the full post to the end of the excerpt.
- It removes HTML tags, line breaks *and* WordPress image captions.
If you'd like the plugin:
- Download the zip file.
- Unzip it and upload to your wp-content/plugins folder.
- Activate the plugin in WordPress admin.
- Set your preferences in Settings > Better Excerpt.
Please note: requires PHP5 and WordPress 2.8.
PS: this is the first plugin I've ever properly released and I'm nervous
If you're using it, please leave me a link - I'd love to see!
Update: It's now in the repository here.
Excerpts of varying lengths
From version 0.2 of the plugin (19th September 2009), it's possible to have excerpts of varying lengths: so for example, if you want a "featured post" on your front page to have a longer excerpt than a dozen other posts, you can do that. Call the function manually like this:
<?php echo better_excerpt($length, $ellipsis, $before_text, $after_text, $link_to_post, $link_text); ?>
for example
<?php echo better_excerpt(500, '...', '
', '
' 'yes', 'read more'); ?>
obviously replacing the variables with your own values. The value of link_to_post should be 'yes' or 'no'. All variables need to have values, so if you don't want any - e.g. - code before or after the extract, you need empty inverted commas ''
I wanted HTML....
The plugin removes HTML. It is DESIGNED to remove HTML. That's what the excerpt does. If you want to leave HTML intact, please use the "more" tag instead.
Tags: my plugins, the_excerpt









Fabulous! Rated it 4 stars on wordpress.org - am using it on a wordpress.org site currently in development (sorry - no link yet!). Would have rated it 5 stars, but I got an error upon Saving the Options on a Wordpress MU site that said the Options page could not be found - whoops!
Thanks Cas, hadn't even tested it on MU!
Ran into a problem and have a question. I seriously suspect this is entirely my fault, but thought you might be able to provide input to troubleshooting this error.
Blog is here: http://mhanson.com
I added excerpt php to my 'single.php' page. The link created by your plug-in reloads the page. I tried moving excerpt php to archive and then tried creating a manual link in the form you provided with no joy (obviously-it linked to what I suggested, but I couldn't figure out how to link to individual post w/o single.php reasserting itself), just wanted to let you know I did make an effort before whining...so, any suggestions?
Hi Mary,
I'm not quite sure why you would add the_excerpt() to your single.php? Single.php is normally for the entire post - that's how WordPress is set up for it to work anyway. If you put an excerpt of the post on the page that is supposed to show the full post, the full post is never going to show up anywhere.
So yes, the link created would indeed reload the page, because the link is to the post's permalink, which points to single.php. So if you wanted the extract on single.php, the thing to do would be not have the link on there.
Could you explain a bit more about what you're trying to achieve? Normally the_excerpt would be used on index.php or archive.php.
I couldn’t figure out how to link to individual post w/o single.php reasserting itself
Single.php IS the individual post. So I really need to understand why you don't want that involved.
A very fine plugin - thanks for shareing.
Nice plugin.
However, because WP by default strips the HTML I would like to force WP to use the HTML from the post. Otherwise the excerpts are just plain text with no formatting, which looks awful.
Can you help me because I've been searching for an hour for a solution without success?
The plugin quite deliberately strips the HTML, Michael. WordPress does this by default too: it's really what the_excerpt() is designed to be - HTMLless. Otherwise, you have the potential to trim the closing tag from something without closing the opening tag.
e.g. you remove the </a> without removing <a href... etc. or </b> without removing the <b>
So there are two ways around this:
1) the more complicated way: trim the content to the length you want, and then check the excerpt for unclosed HTML tags, and close them.
2) the simple way: only remove some of the HTML tags.The latter is easier to do: it might mess up your validation if you have unclosed paragraph tags, but that will probably be the worst consequence.
Go into Plugins > Edit and select Better Excerpt.
Find the line
$text = strip_tags($text);
You can add a second parameter to strip_tags to allow *some* tags to be kept: so if you wanted to preserve paragraphs and line breaks, you'd alter it to
$text = strip_tags($text,'<p><br>');
More info on strip_tags here
http://php.net/manual/en/function.strip-tags.php
Edit: no, that won't work. Let me do some more digging - I'll add "keep line breaks" in as an extra option in the next release of this plugin.
Re-edit. No, I won't do this. Sorry. It will make me far too responsible for people's irresponsible HTML code. Two alternatives are below. If you want control over your content, stop trying to automate it!
Alternatively I can probably re-write the plugin so it checks for unclosed tags for you, if you can be a little bit more specific (preferably show me examples) about exactly what you want it to do.Another possible alternative for you would be to use the "more" tag on your posts instead of the_excerpt() - that would certainly give you more control over what part of your post gets displayed. Looking at your site now, I guess this is what you're doing, and unless you're going to quite severely shorten the length of the extracts on the home page, it's what I'd stick to, personally.
Or - if you really do need to automate it - perhaps I'd worry less about the length of the extract itself, and take (say) the first two paragraphs from the content. Might that work better for you?
Hi there,
Right at the top of my single post pages which the "Read More" links to i am getting:
Read More" />
Please help.
Looee, you need to leave a link to your blog. If it's on the single post page, it's nothing to do with this plugin (unless you are somewhat bizarrely using the_excerpt() on the single post page?). Leave me a link and I'll take a look.
http://longshotsoundsystem.com
it's in the very top left , in black.
thanks for quick response!
That link just goes to a blank page with no content.
Please try again
So, you have a link to the permalink in the middle of your meta tags in your header. That is because you're using the_excerpt to generate a description meta tag (either via another plugin or directly as part of your theme).
If you wish to carry on using my plugin, you need to switch off the link (in Settings > Better Excerpt) and add the link manually after the excerpt using the_permalink(). (You should probably also remove the HTML before and after the excerpt text.)
Hi Sue,
Thanks again.
I guess it must be my theme (mimbo3) that is using the_excerpt to generate meta info, as i dont have many plugins, pretty sure not one generating meta.
I find the best feature of better excerpt is the the link at the end of the pos.
I am familar with HTML/CSS but PHP is totallly new to me. And i think what your suggesting is to edit some php, somewhere, somehow to do what better excerpt is doing.
Oh well.
onwards....
Well, the alternative is to edit Mimbo’s header.php to get rid of the_excerpt for the meta tag (and then use say All In One SEO to do some more SEO stuff including putting the meta description back).
If you want me to fix it for you, I’m quite happy to do so – but I’ll need to be able to sign in to your WordPress. Drop me a line sue @ blogmum .com if you want.
I tried this plugin in found it very nice!
But I have one problem, I want excerpt in different length for 3 places. One in the "headline", one in the "categories" (in the middle of the main page), and the last one in a widget.
Is there any way to do this without get rid of all of the html codes? (Now I am using a code to trim the excerpt, but it results in plain text)
Thanks for your help.
Hi Astra, a few people have asked for the option to have different lengths - I'll have an updated version of the plugin out in a day or two which will do this for you.
As I've said above, the whole point of the_excerpt() is that it removes HTML codes. If you just want to truncate a post, then it's much better to use the "more" tag.
Exactly what I was looking for, I'm using WordpressMU, so I have the same error Cas has though.
Hello, i am a newbie in wordpress.
I have installed this plugin and activate it, but i still confuse how to make a post which contain read more , would you mind to explain to me how to use this plugin?
Thanks a lot
Hi Daniel,
Thanks for your comment. Looking at the link you've left, you don't seem to have WP properly installed, so to be honest, I would worry about that first. There are a lot of files missing from your install. Once you have the theme up and running, you should find it works automatically (assuming your theme is using the_excerpt).
Hi Sue,
Just wanted to ask about what "astrajp" did above. Does this plugin now allow you to set different excerpt lengths? If not do you know how you can have multiple lengths on one blog?
Cheers
Chris
Chris, I'm just testing the new version which lets you set one overriding default, and then gives you template tags which you can use to set different lengths etc. manually. If The West Wing stops distracting me, will be available either later tonight or tomorrow. If you already have it installed on your blog, you'll get the update notification on your dashboard.
Sweet! Cheers Sue, I'll download and wait for the update.
Thanks
Chris
Goodday, I like the idea of the plugin. I just tried the plugin and got the following error.
"Call to undefined function: strripos() in /homepages/33/d107352028/htdocs/agoodeater/wp-content/plugins/better-excerpt/betterexcerpt.php on line 84"
Clif
Hi Clif,
Talk to your webhosts; if it isn't that, please let me know.
That's covered in the FAQs in the Read Me - it's almost certainly because you're using PHP4 not PHP5. As the latter came out in 2004, I think it's time to upgrade
Hi Sue, your plugin is great! There is one issue I have although, and am wondering if you could possibly add an option for this in a future release...
If you have created a manual excerpt for a post, the plugin overrides it and uses the one it creates automagically. Would it be possible to make the plugin or add an option so that it respects manual excerpts?
Otherwise the plugin is awesome. Thanks!
You know, I just remembered that it also overrides the_excerpt_rss(). So it adds the markup and link to the rss excerpt. It would be nice if you could add an option for it to leave the rss excerpt untouched as well. Thanks again!
Or I should say have it so the markup and link are not added to the rss excerpt, so that it is text only...
Nice one man! Just what I was looking for. One question: there is any possibility of work with different languages?
It only have one text to be be set... Could be that be change?
Thanks in advance!
Aaron - thanks, those things on list for version 0.3.
Guillermo - interesting idea. I don't see why not. Let me have a look into it. Are you using another plugin - e.g. WPML - for your multi-languages?
Wow! That was fast!
Excuse for the call you man! I pretty use to find only man as a developers, that I don´t even look the name.
But nice to see I was wrong!
Im using qtranslate, and it seems pretty popular, and easy to adapt.
Also, I´m having the problem that the plugin is not showing the link
This is the line of code, it looks ok to me...
You have any suggestion?
Okay, I'll look at qtranslate.
Don't see your line of code - but if you're using the manual version of the function, remember you have to echo. If it's not that, let me know.
ooops, I put the line of code using the php tags, so it was deleted
Here is again:
echo better_excerpt(500,'','','','yes','sasklskdlsd');
Try changing 'yes' to '1' - I think I may have documented that incorrectly... oops :blush:
It was that. Perfect!
Let me know if you need beta testers for the language change.
Thanks!
Hey there,
Could you add a rel=nofollow tag to the link generated by your plugin?
Thanks Sue for the quick response. Much appreciated. Looking forward to the next release.
If the post has less characters than that were defined it continues appearing "Reade more". But congratulations. Nice plugin.
(Sorry my english)
André, I think you need to take another look at your theme, what you're saying doesn't sound possible to me. Please make sure that your theme does not also include a link *after* the_excerpt().
It would be amazing if you could integrate Justin Tadlock's get_the_image function in your plugin, like in the Query Posts Widget where you can get posts with thumbnails via widget.
That would be a great way of having a excerpt thumbnail extracted from the firs attached image alongside your great plugin functions.
Cya
It would be amazing if I had time to do all the things with this plugin that people want
But for thumbnails, Marcelo, you should wait for 2.9 because I believe they're coming anyway as part of core.
LOL, I know it was quite a complex request but you never know.
Great news about wp 2.9 anyway! I didn't know.
It's just that archives are so boring and I haven't found an easy way to have thumbs in them.
Thank you for your work!
archives are so boring
Yeah, totally agree. If thumbs don't work like we want in 2.9, I'm doing archives-with-thumbs for a client anyway, so I'll post the code up.
Bingo! Works perfect with qtranslate using the qtranslate language tags for the Link Text.
Hi Sue
It seemed that Better Excerpt was going to do just what I needed - the new design for our homepage only allows about 150 characters for the excerpt, and after one whole day of that the editors are asking not to have to manually create an excerpt for each post. But of course the automatic WP excerpts are too long.
However, like Aaron, I also want to have the option of using manual excerpts, so sadly I'm going to have to look elsewhere for the functionality I need (or hack your code - if I do I'll let you know).
Thanks anyway for your efforts,
Chris
Wow. Why do I feel like I just got fired?
Give me a minute, I can probably fix this one tonight.
Hi Sue,
Thanks for the great plugin. Cutting the at word breaks rather than characters is a nice touch. Have you given any thought to using manual excerpts if they exist? I am using Better Excerpt on for a client who has requested the use of manual excerpts for some posts.
Thanks again.
Hi Sue,
tried the better excerpt plugin Version: 0.2.1 and it works great.
Unfortunately it is seems like its not using manual excerpts.
Is this so , or do i need to "switch something on".
Thanks again for the great work you put into this project.
I'm a little confused about how to get the plugin to work. Sorry if this question is simple.
I've installed the plugin and have updated the settings, but I dont really know what I have to do next so that only the excerpt shows automatically.
Is there something I have to do on the post page or does it happen automatically? ITs not working automatcially for me so I think I am doing something wrong.
Never mind - was not included in the page's code. All sorted now.
Thanks - it works like a charm!
No work in WP 2.9...
It's working in 15 different installs here, so perhaps you could be more specific?
(Incidentally, does anyone have ANY idea how unhelpful "doesn't work" is as a comment? For goodness sake, tell me what isn't working. Give me a link. Do something!!)
Sue, thank you for this nice little plugin!
Certainly Better Excerpt works in WP 2.9.
For a project I changed the readmore-link to "Read more about »my article«... I added "$title = get_the_title();" and "if ($link_to_post == 1) {
$text = $text.' '.$link_text.' »'.$title.'«...';" to the plugin.
Now the Readmore is even better. Maybe this is an option in a later version of Better Excpert (and an option no other excerpt plugin had). Sorry for my bad english
From Munich, Germany
Wolf
Vielen Dank, Wolf!
And thanks to everyone for suggestions. I will get around to the next version of this plugin soon.... just paying work comes first. Sorry, but my cats must eat
Is there a way to use this Plugin to pull FAQs excerpts from posts onto an FAQ page? I would like for people to click on the FAQ page and see a listing of questions, without having to click on the FAQ posting category (I want it as a main menu item). I am thinking there is some code I can put into the FAQ page that will pull up excerpts (just the title... which is the question) onto this page for people to select the question they are looking for answers to. I plan on adding more and more and would like for this feature to be automated... If you have a better suggestion as to how to do it, I would love to hear it. I am willing to pay a little (I don't have a lot) to get this accomplished... sooner, rather than later, would be optimal.
Create a new *page* template, that does this:
~ has a custom posts query that only looks at the FAQs category,
~ and outputs the title and the permalink
Create a new blank page, that uses the above template;
publish it.
Assuming you know a little about WP, it's pretty straightforward.
Some suggestions.
it would be great if there is one more option which would define when better excerpts are used: by default or only when echoing function better_excerpt; this is because if we, for example, want to use better excerpts only in some cases, while in other places where the_excerpt is used it would show default look; this could be done by wrapping add_filter/remove_filter function with if which would check if option is selected or not
you don't follow standard practices in WordPress plugin development, like consolidating options (read more about this here, here and here) or adding default options properly via register_activation_hook
consider internationalization of plugin; read more here and here
for your future developments, consider use of built-in functions for settings pages (read more here, here, here and here)
Meh, seems like you need to know coding pretty damn well if your going to use this plugin, way over my head.
I've installed the plugin, and set up the options but the page is still displaying as normal - i.e. full post.
Do I need to add any code into the index page to call the better excerpt function? Or should it work straight out of the box?
Is there an option in the post page to use the excerpt? I can't find anything though.
Any suggestions where to start looking would be appreciated.