Fix php snippet
- or -
Post a project like this1429
£50(approx. $63)
- Posted:
- Proposals: 12
- Remote
- #2837736
- Awarded
WordPress Developer | Page Speed Optimization | Security | Elementor Expert | Custom themes, plugins | PSD/HTML to WP
Manassas
Website Designer and Developer in PHP,Wordpress,Freekore Framework,Codingnigter,CSS,Jquery and Javascript
Chandigarh
Top rated PHP Web Development | WordPress | Magento | Drupal | OpenCart | PrestaShop | Joomla
Leicester
PPH TOP Website & App Developer✮LOGO & Graphic Designer✮Content Writer✮Translator
Dubai
Experienced Full-Stack Web Developer | Web Designer| WordPress | Shopify | React.js | Vue.js| Brand Logo | UX/UI Designer | PHP
London
Graphic Designer |Experienced Web Designer | Video/Audio Editor | PowerPoint/Keynote | Content Writer |
San Jose
1726988206743442520448884467392552207105075422748042513355255070934314164134406
Description
Experience Level: Entry
I am trying to have the search terms on wordpress highlight.
I am using Ajax Search Pro plugin which doesn't do this. I have tried adding the Highlight Search Terms plugin but it doesn't quite work (nearly but not quite is not good enough!). So I found this snippet:
function highlight_results($text){
if(is_search()){
$keys = implode('|', explode(' ', get_search_query()));
$text = preg_replace('/(' . $keys .')/iu', '\0
.search-highlight { background-color:#FF0; font-weight:bold; }
<?php
}
add_action('wp_head','highlight_results_css');
Here's the link to the snippet: https://perials.com/highlight-searched-terms-in-wordpress-search-results/
This is as close as I have got to an answer BUT. When the search term is wrapped in an anchor tag it reads it as text and not html. Here's an example:
Search Term: Armitage
In the code the snippet above creates this:
Armitage and Smith
Which displays as:
Armitage and Smith
However, if you search for a word that is not in an anchor tag the highlight works.
You can see all this in action at:
http://staging.englishchurchartandarchitecture.co.uk
Examples - search Armitage (wrapped in an anchor) then search Smith (not in an tag)
Can anyone fix the snippet to make it work correctly for tags?
I am using Ajax Search Pro plugin which doesn't do this. I have tried adding the Highlight Search Terms plugin but it doesn't quite work (nearly but not quite is not good enough!). So I found this snippet:
function highlight_results($text){
if(is_search()){
$keys = implode('|', explode(' ', get_search_query()));
$text = preg_replace('/(' . $keys .')/iu', '\0
.search-highlight { background-color:#FF0; font-weight:bold; }
<?php
}
add_action('wp_head','highlight_results_css');
Here's the link to the snippet: https://perials.com/highlight-searched-terms-in-wordpress-search-results/
This is as close as I have got to an answer BUT. When the search term is wrapped in an anchor tag it reads it as text and not html. Here's an example:
Search Term: Armitage
In the code the snippet above creates this:
Armitage and Smith
Which displays as:
Armitage and Smith
However, if you search for a word that is not in an anchor tag the highlight works.
You can see all this in action at:
http://staging.englishchurchartandarchitecture.co.uk
Examples - search Armitage (wrapped in an anchor) then search Smith (not in an tag)
Can anyone fix the snippet to make it work correctly for tags?
Carrdale A.
100% (29)Projects Completed
33
Freelancers worked with
31
Projects awarded
48%
Last project
6 Mar 2023
United Kingdom
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
There are no clarification messages.
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies