Search Excerpt WordPress Plugin

Abstract

Search Exceprt plugin displays text snippets around found keywords, with matches highlighted, in a WordPress search.

Download

Usage

  1. Download the plugin file.
  2. Upload "ylsy_search_excerpt.php" to your WordPress' wp-content/plugins directory.
  3. Activate the "Search Excerpt" plugin in your WordPress admin page.

About

WordPress' searching capability is a bit mediocre at the moment. It splits up the keywords, digs out texts using SQL's LIKE operator, and returns all entries that match the criteria, in a reverse chronological order. Then the default template will display some of these entries, using the template function the_excerpt(), which basically returns the first 55 words, HTML striped.

To have a more functional search, i.e. returning entries in the order of relevance, I think WordPress would need to either try to index the text (which is wha Search Reloaded plugin is about), or utilise MySQL's full-text search functions. Both would require modifying the database layout and change "the loop". Yuk.

To have a more meaningful result display, i.e. returning text snippets surrounding the matches, is however quite easy to achieve. Thus my Sunday evening's little project, "Search Excerpt" plugin that displays more meaningful result than the default first 55 words.

It extracts out the search keywords, scan the content of matched post entries, and display document snippets around the keyword, with that specific keyword highlighted. It adds a filter in get_the_excerpt() function so no modification needs to be done to WordPress' 1.5/2.x theme. For other themes, make sure during "the loop" in search.php, it calls the_excerpt() to display the post entries.

Demonstration

Try to search something in the FuCoder.com blog. For example, search for wordpress. You shall see the keyword "wordpress" highlighted.

Customisation

Highlighted keywords in the post excerpt are wrapped in <strong/> tag with CSS class "search-excerpt". For example,

... I think <strong class="search-excerpt">wordpress</strong> is the best open source blogging software ...

Use your CSS stylesheet to customise the highlight. For example,

strong.search-excerpt {
  background-color: #123456;
}

Implementation

Okay. I gotta admit that I took the short cut, and basically copied the implementation of Drupal's search module over. I guess that is what open source is for :)

Support

Feel free to contact me if you have any question regarding this WordPress plugin.

License

Search Excerpt search plugin is licensed under GNU General Public License (same as WordPress and Drupal).

History

  • 1.2 (2006-07-24)
    • Wrap excerpt inside <p>...</p> even if text snippet cannot be found from tag-stripped content.
  • 1.1 (2006-05-08)
    • Merge in Jam's unicode fixes.
    • Try to be executed before wp_trim_excerpt() to avoid Aleksandar's issue.
    • Use own get_content() function to bypass WP's pagination.
  • 1.0 (2005-08-22)
    • Initial release.