• Resolved bizangodan

    (@bizangodan)


    First off, thanks for making a great plugin! I love the updated excerpt functions that actually show relevant text from the content.

    Only issue I found is that searching for certain words that may come up in scripts will sometimes be included in the new excerpts in the search results.

    For instance if I try to search “document” It will start showing “$(document).ready(function(){…}” in the search excerpt for some pages.

    The site uses acf pro and has many custom acf blocks which have scripts. I just wanted to check if there was a quick fix I could do before separating out all the scripts and enqueue-ing them instead

    Is there anyway to exclude all text within “<script></script>” tags from the queries?

    I’m using a custom theme by the way and have full access to the files.

    • This topic was modified 2 years, 8 months ago by bizangodan.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Epsiloncool

    (@epsiloncool)

    Hi @bizangodan

    As I understood you, the acf blocks you included to pages have some javascript/css code that you want to exclude from the search index and from the excerpts, right?

    The “Strip HTML Tags From Post Contents” flag in the settings allows to remove tag names, but it still make tag content indexed (including <script> and <style> tags).

    So what should we make?

    We can post-process the post content just before the indexing. It will also do the trick for excerpts. Let’s use the code like this:

    
    add_filter('wpfts_index_post', function($index, $post)
    {
    	global $wpfts_core;
    	
    	// This code will work properly only with "Strip Tags Option DISABLED"
    	if ($post && $wpfts_core) {
    		$t = $index['post_content'];
    		if (in_array($post->post_type, array('post', 'page'))) {	// You can add more post_types here
    			// Remove <script> with content
    			$t = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $t);
    			// Remove <style> with content
    			$t = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', "", $t);
    		}
    	
    		// This code will actually strip tags (instead of disabled option)
    		$t = $wpfts_core->contentStripTags($t);
    		$index['post_content'] = html_entity_decode(str_replace('&nbsp;', ' ', $t));
    	}
    	
    	return $index;
    }, 20, 2);`
    

    This should definitely help.

    Plugin Author Epsiloncool

    (@epsiloncool)

    Sorry, forgot to notice @bizangodan

    you should uncheck “Strip HTML Tags From Post Contents” option (make it OFF) to make this code working properly.

    Thread Starter bizangodan

    (@bizangodan)

    @epsiloncool
    Thanks so much for the quick response

    That seemed to work!

    Thanks again, I appreciate your time on this

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Prevent search from including javascript’ is closed to new replies.