• 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 596 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 (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

  • Is it Good to Run Apache Web server and MySQL Database on Separate Cloud Servers? March 27, 2023
  • Advantages of Cloud Server Over Dedicated Server for Hosting WordPress March 26, 2023
  • Get Audiophile-Grade Music on Your Smartphone March 25, 2023
  • Simple Windows Security and Privacy Checklist for 2023 March 24, 2023
  • 7 Best Artificial Intelligence (AI) Software March 24, 2023

About This Article

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

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

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

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

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT