RavanH / highlight-search-terms

Lightweight jQuery search terms highlighter script for WordPress
https://status301.net/wordpress-plugins/highlight-search-terms/
GNU General Public License v3.0
8 stars 3 forks source link

Add support the_permalink in search results #6

Closed marcissimus closed 3 years ago

marcissimus commented 4 years ago

Some templates for search results use the_permalink() and then previous url filters won't work in case of custom post types with rewrite rules (for example Easy Digital Downloads, post type "downloads").

PS: I love the simplicity of this plugin!

RavanH commented 4 years ago

Hi, thanks for contributing :)

My question: how come that the post_link filter does not work for you? The function the_permalink calls get_the_permalink which uses either the post_link filter or the page_link filter (through get_page_link) or the post_type_link filter (through get_post_permalink)...

What I found testing on a site using EDD and the Vendd theme was that the search template uses a custom loop which the does not pass the in_the_loop() if statement... It's actually an Vendd theme bug (or feature?)

mbtools commented 1 year ago

Sometimes things take a while... Just in case someone else runs into the same problem:

Symptom

The search result contains links to posts which do not include the search query ?hilite=.... Therefore clicking the link will not highlight the search term in the detailed post.

Root Cause

Another plugin or theme implemented the_permalink filter hook and replaced the URL without taking the URL query into consideration.

In my case, it was a theme from https://cpothemes.com/, which provides custom post types that let you configure a so called "Custom URL". If the custom URL is set to any value, the search query will be lost when such custom post shows up in the search results. I consider this a bug but the setting might as well be called a feature.

Workaround

I added an if-statement into search.php to skip over posts that have a "Custom URL" setting (since they are just redirecting to another page).

RavanH commented 1 year ago

Hi Marcus, thanks for updating and adding more details. On the next update with 6.2 compatibility, I'll do some testing with the the_permalink hook...

mbtools commented 1 year ago

great. here's another test case: i had permalinks returned in local format like /my-category/page so it's not necessarily a complete URL.