• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Highlight search results in WordPress with jQuery

By Abhishek Ghosh March 26, 2011 6:53 pm Updated on March 26, 2011

Highlight search results in WordPress with jQuery

Advertisement

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.

Advertisement

---

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

 

Vim
1
2
3
4
5
6
7
8
9
10
11
12
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:

 

Vim
1
2
3
<php INSERT CODE HERE
//Uncomment and do not use this Line break
//?>

 

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:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<style type="text/css" 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.html (this.html (). 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 make 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:

Vim
1
$ ("#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 page in the browser of your choice.

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

 

Vim
1
.hls { background: #D3E18A; }

 

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

The search module integrated basic WordPress offers no visual 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
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Highlight search results in WordPress with jQuery

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • How to Correctly Include jQuery-UI Effects on WordPress

    How to correctly include JQuery-UI effects on WordPress on your existing Theme without much compromise on Page Loading Speed? It is made easy with our Guide.

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

vpsdime

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Cloud-Powered Play: How Streaming Tech is Reshaping Online GamesSeptember 3, 2025
  • How to Use Transcribed Texts for MarketingAugust 14, 2025
  • nRF7002 DK vs ESP32 – A Technical Comparison for Wireless IoT DesignJune 18, 2025
  • Principles of Non-Invasive Blood Glucose Measurement By Near Infrared (NIR)June 11, 2025
  • Continuous Non-Invasive Blood Glucose Measurements: Present Situation (May 2025)May 23, 2025
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2026 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy