Term Highlight

Current version: 2.1.14 View Changelog

Download available with active license

⚠️ Note: This extension is DEPRECATED as of SearchWP version 3.0 and will be removed soon ⚠️

Please see this documentation:

When active, Term Highlight will automatically scrub the Title and Excerpt returned from searches and wrap search terms with a span, allowing you to highlight them in such a way that matches your site design well.

Term Highlight also includes a function allowing you to retrieve an excerpt from the post_content or from a Custom Field that contains at least one of the search terms. See usage below.


Installation for this Extension is the same as all other Extensions. Download the zip file from your Account, upload and install as you would any other WordPress plugin. Term Highlight will show up in your Plugins list in the WordPress administration area, and you can activate it.

Once activated, Term Highlight will automatically highlight terms within the Title and Excerpt for native WordPress searches. Highlights are applied by wrapping terms in <span class="searchwp-highlight" />, which you can target in your CSS.

Term Highlight will also automatically override calls to the_excerpt() by replacing that content with an excerpt that contains at least one search term from the main content (e.g. the main content field, not Custom Fields). It will also highlight terms in this content. If you would like to disable or customize this functionality, please see the filters below.

You can take it one step further by utilizing Term Highlight to generate an excerpt from a number of potential sources (including Custom Fields) by automatically filtering calls to the_excerpt() on search results pages like so:

function searchwp_term_highlight_auto_excerpt( $excerpt ) {
global $post;
if ( ! is_search() ) {
return $excerpt;
// prevent recursion
remove_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
$global_excerpt = searchwp_term_highlight_get_the_excerpt_global( $post->ID, null, get_search_query() );
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
return wp_kses_post( $global_excerpt );
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
view raw functions.php hosted with ❤ by GitHub

If instead you want to manually replace the calls you can use something like this:

/* SearchWP Term Highlight offers an alternative to the_excerpt()
If you're looking to generate any sort of excerpt based on one of the following:
- the actual excerpt
- the generated excerpt from the post content
- the post content itself
- string-based custom field values
this function is for you. It will try to find one of the above (in that order)
and output something with at least one search term in it (if possible)
// echo the excerpt (designed to be used IN PLACE OF the_excerpt
if( function_exists( 'searchwp_term_highlight_the_excerpt_global' ) ) {
// return the excerpt (designed to be used IN PLACE OF get_the_excerpt
if( function_exists( 'searchwp_term_highlight_get_the_excerpt_global' ) ) {
$excerpt = searchwp_term_highlight_get_the_excerpt_global();
view raw gistfile1.php hosted with ❤ by GitHub

Supplemental Search Engines

Term Highlight does not automatically highlight terms for Supplemental Search Engine searches! You will need to customize your template by sending your content through the highlighter before outputting it, like so:

// this snippet is based on SearchWP's sample Supplemental Search Engine results template found here:
// **************************** NOTE ****************************
// this snippet is just a portion of your search results template
// **************************** NOTE ****************************
// these are the search terms
$query = isset( $_REQUEST['swpquery'] ) ? sanitize_text_field( $_REQUEST['swpquery'] ) : '';
$highlighter = false;
if( class_exists( 'SearchWP_Term_Highlight' ) ) {
$highlighter = new SearchWP_Term_Highlight();
<?php foreach ( $posts as $post ): setup_postdata( $post ); ?>
<div class="post">
<a href="<?php echo get_permalink(); ?>">
// highlight the title
$title = get_the_title();
if( $highlighter ) {
$title = $highlighter->apply_highlight( $title, $query );
echo wp_kses_post( $title );
// output an excerpt
$excerpt = get_the_excerpt();
if( $highlighter ) {
$excerpt = $highlighter->apply_highlight( $excerpt, $query );
echo wp_kses_post( $excerpt );
<div class="custom-field-content">
// output highlighted content from a Custom Field
$custom_field = get_post_meta( get_the_ID(), 'my_custom_field_key', true );
if( $highlighter ) {
$custom_field = $highlighter->apply_highlight( $custom_field, $query );
echo wp_kses_post( $custom_field );
<?php endforeach; ?>
view raw gistfile1.php hosted with ❤ by GitHub

NOTE: the snippet above is meant to represent the customization you would need to do to the sample Supplemental Search Engine results template replacing the search results output ONLY, it is not an entire page template.


There are a couple of filters to note when using Term Highlight:

Set whether Term Highlight automatically replaces the_excerpt() with an excerpt containing search terms. Default is true, to disable:

add_filter( 'searchwp_th_auto_filter_excerpt', '__return_false' );
view raw gistfile1.php hosted with ❤ by GitHub

Define the number of words Term Highlight will include when building an excerpt that includes at least one search term. Default is 55.

function my_searchwp_th_num_words() {
// use 75 words instead of the default 55
return 75;
add_filter( 'searchwp_th_num_words', 'my_searchwp_th_num_words' );
view raw gistfile1.php hosted with ❤ by GitHub

Tell Term Highlight to skip term highlighting for certain search engines. Default is none.

function my_searchwp_th_excluded_engines( $load_posts, $search_args ) {
// DO NOT highlight terms when using the 'my_supplemental' search engine
return array( 'my_supplemental' );
add_filter( 'searchwp_th_excluded_engines', 'my_searchwp_th_excluded_engines', 10, 2 );
view raw gistfile1.php hosted with ❤ by GitHub

If a post is password protected, Term Highlight returns a custom string. Default is WordPress core default: There is no excerpt because this is a protected post..

function my_searchwp_th_password_required_message() {
return __( 'This entry is private.' );
add_filter( 'searchwp_th_password_required_message', 'my_searchwp_th_password_required_message' );
view raw gistfile1.php hosted with ❤ by GitHub



  • [Fix] Fixes an issue where highlighting was applied when no search term was provided
  • [Fix] Restricts global excerpt generation according to applicable meta keys only
  • [Improvement] Better highlighting when terms are flanked with punctuation


  • [Fix] Now finds proper highlight when only punctuation-flanked stem is found


  • [Fix] Fixed an issue with excerpts generated from multidimensional array meta values


  • [Fix] Fixed an issue with loss of capitalization/formatting when highlighting from a meta value


  • [Improvement] Improve performance of repeated calls to highlighting method per request


  • [Fix] Fixed an issue where exact phrase matches would output Array


  • [Fix] Clean up PHP Warning


  • [Fix] Fixes an issue where partial matches may result in less accurate global excerpts
  • [Improvement] Improved exact match processing
  • [Update] Updated updater


  • [Update] Highlighting no longer auto-applies for searches in the Dashboard


  • [Fix] Fixed improper variable initialization that may have resulted in an Error (props Vlad)


  • [Improvement] Better support for serialized data when building global excerpt
  • [Fix] Prevent global excerpt generation from defaulting to filename


  • [Fix] Fixed an issue with overly aggressive tokenizing which may have resulted in a loss of formatting
  • [New] New filter searchwp_term_highlight_occurrence to offset where highlighting begins (default is occurrence 1)
  • [Fix] Improved handling of whole match check, resolved PHP Warning
  • [Update] Updated updater


  • [Fix] Better handling of operations with empty search string
  • [Fix] Fixed PHP Warning
  • [Fix] Fixed an issue when finding global excerpts with multiple terms flanked by encoded punctuation


  • [Improvement] Check for whole matches before parsing individual search terms
  • [Improvement] Better handling of pattern matches
  • [Update] Updated updater


  • [New] New filter searchwp_th_auto_highlight_content to disable automatic highlighting
  • [Improvement] More priority given to exact search matches
  • [Improvement] Improved performance of highlighting operation


  • [Fix] Better highlight restriction when other SearchWP extensions are active
  • [Fix] Better tokenizing when extracting highlight matches


  • [Improvement] Apply appropriate filters for better interoperation with other SearchWP Extensions
  • [Change] More appropriate initialization timing
  • [Update] Updated updater


  • [Fix] PHP Warning cleanup


  • [Fix] PHP Warning cleanup


  • [Fix] Fixed an issue that forced highlights to be lowercase


  • [Improvement] Better Shortcode handling
  • [Improvement] Better UTF-8 support
  • [Improvement] Better handling of keyword stems
  • [Fix] Fix an issue where redundant filter calls were in place


  • [Fix] Fixed a bug that gave too much priority to manually populated Excerpts when the Excerpt did not have a highlight match but the post content did


  • [Fix] Fixed a bug that ignored a specified Custom Field from which to generate a global excerpt


  • [New] New filter searchwp_th_partial_matches which enables highlighting of partial term matches


  • [Fix] Fixed PHP Warning, better short circuit when post ID is not defined


  • [Improvement] Support for automatic updates based on your SearchWP license key


  • [Improvement] Better word matching at boundaries
  • [New] Added Shortcode handling


  • [New] Added ability to pass search query down the pipe for global excerpt generation
  • [New] Added searchwp_th_query filter to allow devs to override what is considered for the term


  • [Fix] Fixed an issue where highlighting would not take place if the class weren't instantiated properly


  • [Fix] Warning cleanup
  • [New] Allow specification of Custom Field name when globally generating an excerpt


  • [New] Added a new function to allow generation of an excerpt based on any string-based Custom Field if the default excerpt doesn't produce a match


  • [Improvement] Reduced aggressiveness matching conditional


  • [Improvement] Better handling of terms to highlight


    • Initial release

Want to make your search awesome right now?

More than 30,000 sites have chosen SearchWP!

You can utilize all of the content that’s gone unrecognized by native WordPress keyword search instantly with SearchWP.

Get SearchWP for just $99

  • Committed Support
    If you need help, support is fast, friendly, and here for you
  • Streamlined Setup
    Installation and setup that’s optimized for speed
  • Great Documentation
    Helpful, clear, and usable documentation is a priority

See what SearchWP customers have to say

  • “My company is using Wordpress internally as Knowledge base system. We choose to not use menu and base everything on the search engine like Google does. SearchWP is an extremely efficient tool to find what we need quickly. I would recommend it.”

  • “SearchWP have been a lifesaver when clients have hi demands on how search results should be weightet. An essential plugin for all modern WP sites.”

  • “Love SearchWP. Love your service. Couldn’t recommend it enough, as I often drop the name SearchWP when talking to my WordPress Developer Associates. The plugin is updated often, it’s reliable and support is amazing. It’s very easy to use and setup!!”