Installation instructions
To make it work, you will need to take up to three steps, depending on your wishes and WordPress theme. (I) A normal installation and activation procedure; (II) Make sure your theme uses any of the recognized classes or ID’s for the post content div so that the script knows where and where not to look for search terms; and (III) optionally add CSS styling rules to get highlighting for older browsers that do not support HTML5 like Internet Explorer 8 and below.
I. Go to Plugins > Add New and search for “highlight search terms” in your WordPress admin or follow these basic steps.
- 01Download archive and unpack.
- 02Upload (and overwrite) the /highlight-search-terms/ folder and its content to the /plugins/ folder.
- 03Activate plugin on the Plug-ins page
II. In most up to date themes (including WP’s own Default theme) post and page content is shown inside an ARTICLE element or DIV with class hentry . This is recognized by the hilite script, which means search terms found in post and page content will be highlighted but not similar terms that coincidentally reside in the page header, menu, sidebar or footer.
If your current theme does not use these common designations (yet), this plugin will look for some other possible tags like #content , MAIN , #wrapper (which might include the header, sidebar and footer areas) and finally the BODY .
If this causes issues on your theme, see the last of the FAQ’s for ways to make it work.
III. Optionally add at least one new rule to your themes stylesheet or the Custom CSS editor to style highlightable text.
For example use .hilite { background:#D3E18A; } to get a moss green background on search terms found in the content section (not header, sidebar or footer; assuming your Theme uses a div with class “hentry”).
Please find more examples in the FAQ’s below.
I do not see any highlighting!
This plugin has no configuration options page and there is no predefined highlight styling. For any highlighting to become visible in browsers that do not support HTML5 like Internet Explorer 8 or older, you have to complete step III of the installation process. Edit your themes stylesheet (style.css) or the WordPress theme customizer Custom CSS tab to contain a rule that will give you exactly the styling that fits your theme.
If that’s not the issue, then you might be experiencing a bug or plugin/theme conflict. Time to get some Support 🙂
I want to customize the highlighting but have no idea what to put in my stylesheet. Can you give me some examples?
Go in your WP admin section to Appearance > Customize > Custom CSS and add one of the examples below to get you started.
For a moss green background highlighting:
.hilite { background-color: #D3E18A; }
Yellow background highlighting:
.hilite { background-color: yellow; }
A light blue background with bold font:
.hilite { background-color: #9CD4FF; font-weight: bold; }
Orange background with black font:
.hilite { background-color:#FFCA61; color: #000000; } Please give me more advanced CSS examples
If you want to give different terms used in a search phrase a different styling, use the class “term-N” where N is a number starting with 0, increasing 1 with each additional search term, to define your CSS rules. The below example will make every instance of any term used in the query show up in bold text and a yellow background except for any instance of a second, third and fourth term which will have respectively a light green, light blue and orange background.
/* Default highlighting, but bold text. */ .hilite { background-color: yellow; font-weight: bold; } /* Moss green highlighting for second search term only. */ .term-1 { background-color: #D3E18A; } /* Light blue highlighting for third search term only. */ .term-2 { background-color: #9CD4FF; } /* Orange highlighting for fourth search term only. */ .term-3 { background-color:#FFCA61 }
Keep in mind that for the first search term the additional class “term-0” is used, not “term-1”!
Terms outside my post/page content get highlighted too
The script will search through your page source code for viable sections that usually contain page or post content, most commonly used in WordPress themes. Like ARTICLE or a DIV with class “hentry”. If that is not available, the script will look for other commonly used divs like #content, #main, #wrapper. However, in your particular theme, none of these divs might be available…
Let’s suppose your theme’s index.php or single.php has no <div <?php post_class() ?> ... > but wraps the post/page content in a <div id="someid" class="someclass"> ... </div> . This will not be recognized by the script as a post/page content container and the script will default to highlighting starting with the BODY element.
This will result in terms getting highlighted in the header, menu, sidebar and footer too.
You can do one of three things to solve this:
A. Change your theme templates like single.php, page.pho and search.php so the post/page content div has a class “hentry” (you can append it to existing classes with a space like class="someclass hentry" ).
B. Add a filter in your theme’s functions.php (or a seperate plugin file) like this:
add_filter( 'hlst_selectors', function( $selectors ) { // custom theme selectors (change and append as needed) $my_selectors = array( 'div.someclass' ); return $my_selectors + (array) $selectors; } );
C. Switch to a theme that does abide by the current WordPress conventions 🙂