Highlight search results in WordPress with jQuery


This PHP hack for WordPress enables you to highlight keywords in the search results of your WordPress site. We willHighlight search results in WordPress use jQuery, a language built natively inside of WordPress.

 

Adding a PHP command in functions.php

From the Theme Editor, open the file called functions.php or open by your favorite FTP client by locating it in the directory / wp-content/themes/Your-Theme /

Using an FTP client can perform a backup prior to editing the file directly which allows you to undo a wrong manipulation.

Then paste the following lines of code to the file if you use the theme Twenty Ten (default):

 

hls_set_query function () {
$ Query = attribute_escape (get_search_query ());if (strlen ($ query)> 0) {
echo '
<script type="text/javascript">
hls_query var = "'. $ query.";
</ Script>
';
}
}hls_init_jquery function () {
wp_enqueue_script ('jquery');
}add_action ('init', 'hls_init_jquery');
add_action ('wp_print_scripts', 'hls_set_query');

For other themes, it is often necessary to include these tags PHP according to this model:

 

<? Php INSERT CODE HERE?>

 

Remember to Update the file to save the changes.

 

Editing file header.php

Also in the theme editor, open the header . php file that corresponds to the header of your site.
Paste the code lines below just before the tag we just look for:

<style type="text/" media="screen">
. Hls {background: # D3E18A;}
</ Style>
<script type="text/javascript">
jQuery.fn.extend ({
highlight: function (search, insensitive, hls_class) {
var regex = new RegExp ("(<[^>]*>)|( \ \ b "+ search.replace (/([-.*+?^${}()|[ \] \ / \ \] ) / g, "\ \ $ 1") +")", insensitive? "ig", "g");
return this. (this. (). replace (regex, function (a, b, c) {
return (a.charAt (0) == "<")? a: "<strong class=\""+ hls_class +"\">" + c + "</ strong>";
}));
}
});
jQuery (document). ready (function ($) {
if (typeof (hls_query)! = 'undefined') {
$ ("# Post-1"). Highlight (hls_query, 1, "HLS");
}
});
</ Script>

 


Adjustments required customized color

To this snippet working, you must specify the class or ID of the tag that will display the search results.

Twenty To Ten, it is #post-1 as indicated in this line of code:

$ ("# Post-1"). Highlight (hls_query, 1, "HLS");

We recommend using the Firebug extension for Mozilla Firefox which allows you to quickly identify the tag in question – also possible via the display of the source code of the in the browser of your choice.

To the highlight color, the hexadecimal value for the following line:

 

.hls { background: #D3E18A; }

 

This value can be obtained easily via any editor or as a Firefox extension ColorZilla.

The search module integrated basic WordPress offers no identification of search terms in the search results page. The technique presented here will fill this gap unless you’d prefer to integrate the Google search engine to your site !

 

Signature


Incoming search terms:

highlight book,wordress colorize search result
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
About Abhishek

Abhishek Ghosh is an Orthopedic Surgeon, Inventor with 216 Patents, Current editor of The Customize Windows Media Group. You can follow and know more about Dr. +Abhishek Ghosh on Google Plus and follow on Twitter as @AbhishekCTRL.

Speak Your Mind

*