SearchWP Highlighting
SearchWP includes a built-in highlighting feature that identifies and emphasizes search terms found within post content. It displays a snippet of text containing the highlighted keywords on the search results page, allowing users to easily locate the relevant portion of content. This feature is particularly useful for large media documents, such as multi-page PDFs, as it scans through the complete document content, locates instances of the search term, and generates an excerpt from the section containing those keywords.
The highlighting functionality is fully compatible with custom field plugins like Advanced Custom Fields (ACF), allowing it to generate excerpts from search terms found in ACF text fields and other custom field content.

Enabling Highlighting
The highlighting feature can be activated through the SearchWP General Settings page in your WordPress admin area. Once enabled, SearchWP will automatically generate custom excerpts that highlight matching search terms in your results.
Content Processing Order
The highlighting system processes source attributes in a specific sequential order to determine where to generate excerpts from. This prioritized approach ensures efficient processing and consistent results across your search implementation.
Post Excerpt: SearchWP first checks for manually created post excerpts. If a post has a custom excerpt defined, this content is evaluated first for the presence of search terms.
Post Content: If no excerpt exists or no keywords are found in the excerpt, SearchWP examines the main post content field for instances of the search terms.
Document Content: For media files such as PDFs, SearchWP processes the extracted document content to locate search terms within the file’s text.
Custom Fields: Finally, SearchWP checks custom field content for the presence of search terms if they haven’t been found in any of the previous attributes.
If any searched keywords are found in one of these attributes, the subsequent attributes will not be processed. For example, if the post excerpt contains the searched keywords, SearchWP will not examine the post content or custom fields for generating an excerpt.
Note: When multiple instances of a keyword exist within a single attribute field, the first occurrence of the keyword will be used to generate the excerpt.
Excerpt Generation
Once SearchWP locates the content containing the keywords to highlight, it generates an excerpt by extracting a portion of text before and after the keywords. This contextual approach ensures that users see not just the matching term but also the surrounding content that provides context for the match. The excerpt length is controlled by your WordPress settings and can be customized using the WordPress excerpt_length filter hook, as documented here.
To modify the excerpt length specifically for SearchWP Live Search results, you can implement the following customization:
| <?php | |
| // Set excerpt length to 20 words for live search results | |
| function swp_excerpt_length( $length ) { | |
| if ( did_action( 'wp_ajax_searchwp_live_search' ) || did_action( 'wp_ajax_nopriv_searchwp_live_search' ) ) { | |
| return 20; | |
| } | |
| return $length; | |
| } | |
| add_filter( 'excerpt_length', 'swp_excerpt_length', 9999 ); |
Styling Highlighted Terms
The default highlighting markup includes the searchwp-highlight CSS class, which you can target with custom styles to control the visual appearance of highlighted search terms. You can add custom CSS rules to your theme’s stylesheet to modify the background color, text color, font weight, or other visual properties of highlighted keywords. This allows you to ensure that highlighted terms are clearly visible and match your site’s overall design aesthetic.

