WordPress auto excerpts from post content?

I’m trying to create an auto generated post excerpt from the current page’s post content using a function in my theme’s header file. The post excerpt will be used as the page’s meta description. Can someone give me an idea of how you might go about this once you’ve got the post content into a string variable?

The somewhat tricky part is that, in order to predict a viable stopping point for the post excerpt, I’d like to specify that the cutoff point be the end of the first paragraph of text.

Read More

And for that reason, it does not make sense to load the entire post content into the string I’m using. Can I grab the first paragraph without having to load the entire post content string?

And I’m not certain how to test for that in php. Would regex be the only way?

Related posts

Leave a Reply

1 comment

  1. You can’t parse HTML with regex. Posts are stored formatted in HTML (i.e. <p></p> <br /> etc).

    I’m also assuming you are implementing this on a blog with lots of existing posts.

    What you can do is:

    1. Retrieve the post and run it through an XML Parser. Grab the first paragraph. This is incredibly expensive for such a simple task.

    2. Use a quick tag in the post to denote the excerpt stop point, strip HTML from everything to the left of it. Similar to the <-- more --> tag.

    3. Store an excerpt with each post, I believe WP already has facilities for that.

    It would be much, much easier if you could simply select the excerpt without having to do any additional fiddling in order to use it, so the time to handle it is when a post is saved.

    So, if you can initially select each post, parse it, get the first paragraph and insert it into another table, then have your plugin do that when each new post is saved, you’re home. Naturally, you’d update the same if a post was edited (making that optional).

    Just please, please, please don’t introduce a plugin in WP that uses regex to parse a context free language. Its just asking for trouble.