• 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

This Article Has Been Shared 710 Times!

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

  • How to customize Feedburner email subscription form

    Using CSS, you can customize your Google Feedburner subscription form to give a nice look matched with your Wordpress theme.

  • 30 ways to increase the readability of your blog

    One of the most important aspect in the design of a blog is readability, how easy it is to read your content. People come to your blog to read articles, and if they are easier to read, the better.

  • Making of WordPress theme : softwares and browsers addons

    List of the most useful tools needed for designing and coding a Wordpress theme.

  • Best features of WordPress

    This article Best features of Wordpress describes the features which has established Wordpress in the top position in search engine war.

  • First AdSense Unit is the Key to Make More Money from Google AdSense

    First AdSense Unit should be the Google AdSense Unit which has the Highest CTR. Here are tips to optimize your First AdSense Unit make more money from it.

Additionally, 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…

 

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 (22.1K 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

  • Ways To Make Sure Your Online Course Outshine Others July 3, 2022
  • Will Smart Factories Become the New Assembly Line? July 2, 2022
  • The Cost of Doing Business as a Handyman July 1, 2022
  • Samsung Galaxy S22 Ultra: Long Term Review June 30, 2022
  • How to Make the Most of Your S Pen (S22 Ultra) June 29, 2022

About This Article

Cite this article as: Abhishek Ghosh, "Highlight search results in WordPress with jQuery," in The Customize Windows, March 26, 2011, July 4, 2022, https://thecustomizewindows.com/2011/03/highlight-search-results-in-wordpress-with-jquery/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

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

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