Not searching inside elementor templates content

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Not searching inside elementor templates content

This topic contains 2 replies, has 2 voices, and was last updated by eprjweb eprjweb 7 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38985
    eprjweb
    eprjweb
    Participant

    Good morning,
    we’re starting to set up the plugin on a site that we didn’t originally do and inherited instead. On the website there is a woocommerce without sales and each product has its own template associated and created with elementor. (template> theme builder> single product).
    Some products have codes within their template, codes entered through elementor’s text widget.
    We need the search to find these internal strings as well. Currently it doesn’t seem to work.
    Could you, please, help us with the configuration?

    https://stefanop65.sg-host.com/shop/sollevatori/sollevatori-auto/doppia-forbice/thor/ (a sample page with Cod. Art. EK36E.LV8.01)
    in the attachments you will find the unexpected behavior of the plugin

    Attachments:
    You must be logged in to view attached files.
    #38989
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Thank you very much for the details.

    Interestingly, this may not be possible. I have checked the Index table core, and no matter how I tried to retrieve the content of that post, it was always empty.
    – Using WP_Query fails
    – Using get_post($post_id) fails
    – After some googling, I found a method to fetch the elementor contents directly from that page:

    $frontend = new \Elementor\Frontend();
    $contentElementor = $frontend->get_builder_content_for_display( $post_id, true );

    This returns an empty string as well..

    I am not sure if this is an issue with Elementor, or this is how it supposed to work, but there does not seem to be any way of fetching the content of the template pages via the post ID (in this case product ID, but it does not matter). It always returns nothing at all.

    The solution would be the code above, but plugging the template ID instead of the post ID. However as far as I know, there is no simple way of getting the single template ID used with the post, or any other way of getting the post content (?).

    The only way to bypass this whole issue is to parse the actual HTML output of the missing pages, and then index the required parts. For that, I have added the following code to the functions.php file in your theme directory:

    add_filter('asp_post_content_before_tokenize_clear', 'asp_post_content_from_url', 10, 2);
    function asp_get_html_from_url( $url ) {
    	if ( !class_exists('domDocument') )
    		return false;
    
    	$content = file_get_contents($url);
    	$dom = new domDocument();
    	$return = '';
    	if ( function_exists('mb_convert_encoding') )
    		@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
    	else
    		@$dom->loadHTML($content);
    	$dom->preserveWhiteSpace = false;
    	$main = $dom->getElementsByTagName('main');
    	foreach ( $main as $node ) {
    		$return .= $node->ownerDocument->saveHTML($node);;
    	}
    
    	// Still no image
    	return $return;
    }
    function asp_post_content_from_url($content, $the_post) {
    	if ( $content == '' && !empty(get_post_permalink($the_post->ID)) ) {
    		$content = asp_get_html_from_url(get_post_permalink($the_post->ID));
    		var_dump(get_post_permalink($the_post->ID));
    	}
    	return $content;
    }
    
    add_filter('asp_search_phrase_before_cleaning', 'asp_replace_characters', 10, 1);
    add_filter('asp_query_args', 'asp_replace_characters', 10, 1);
    
    function asp_replace_characters( $s ) {
      $characters = "',._-?!"; // Type characters one after another
      $replace_with = ' ';     // Replace them with this (space by default)
    
      if ( is_array($s) ) {
        if ( isset($s['s']) && !$s['_ajax_search'] ) 
          $s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);      
      } else {
        $s = str_replace(str_split($characters), $replace_with, $s);
      }
    
      return $s; 
    }

    Please keep in mind, that this is not a good solution at all, and it may not work in every case as it should.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39003
    eprjweb
    eprjweb
    Participant

    Thnak you for helping, Ernest.
    Now it seems to work fine 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.