This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Search results content

Viewing 15 posts - 1 through 15 (of 35 total)
  • Author
    Posts
  • #53110
    erikaerika
    Participant

    Hi there
    I have pdfs that I am indexing. Please let me know how search excerpts can be shown from pdfs with a matching keyword and with a clickable link to the pdf which is in our woocommerce show.

    For example. Search for “adoption”. Results:

    Issue #1. With adoption is is recommended that children be treated with the utmost confidentiality…
    Issue #99. In homes where adoption is a …
    Issue #400. Adoption is a common practice in the UK … etc

    Maybe you can point me to a tutorial or do it for me please? Admin access provided.

    regards
    Erik

    #53111
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Erik,

    I recommend watching the video on this page about file search.

    PDFs however can’t be displayed in a WooCommerce products widget – Elementor does not allow that, nor any other widget either. I guess that’s what you are trying to do, but Elementor will automatically remove them as the products widgets should only every display products and nothing else (and media files are not products).

    The video tutorial on the link above will explain everything about searching files and file contents 🙂

    #53123
    erikaerika
    Participant

    Great, thank you! Works well…and yes, I want to list the products that come up from the search. If I add the pdfs as products, is it not possible to search products and not attachments in the same way? At the moment, the pdfs are listed with the context and if you click it goes directly to the pdf and not the “add to cart” page.

    #53131
    Ernest MarcinkoErnest Marcinko
    Keymaster

    If you are using the elementor products widget, then for that you have to select the products to search and not the PDFs.

    If you use the plugins onwn results container (similarly as it is in the tutorial and demo), then there is a chance. If the PDFs were uploaded to the products as attachments, then by changing this option WordPress will return the parent URL, which should be the product. Therefore the plugin will display the PDFs as results, but link to the product page itself.

    #53168
    erikaerika
    Participant

    Thanks. No, I can choose to use the elementor widgit or not. We are still in the testing phase so there is flexibility. I will upload the PDF as an attachment as you suggest. I assume then I choose product and not attachment for “Post types to index”?

    #53169
    Ernest MarcinkoErnest Marcinko
    Keymaster

    I assume then I choose product and not attachment for “Post types to index”?
    Yes – exactly.

    #53172
    erikaerika
    Participant

    Thanks Ernest. I notice that when I choose product and not attachment, then the option to use the External File indexing feature goes away. But I do want to index those for the pdfs that are attached to the product…and to display the excerpt.

    • This reply was modified 1 year, 3 months ago by erika.
    • This reply was modified 1 year, 3 months ago by erika.
    #53177
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You are welcome!

    I don’t think there is a way to do that via settings I’m afraid. Products are not actually the files, they are two separate entities. There might be some hacky way to get around it, but I’m not sure.

    Can you add temporary SFTP access, I might have to test some customized code to see if there is any way to do this.

    #53179
    erikaerika
    Participant

    Thanks for the response. You can try log in using the credentials I posted when I created this ticket. There is a File Manager plugin that will give you access to the files. However, this is our live site so…do you think your code will break anything?

    #53186
    Ernest MarcinkoErnest Marcinko
    Keymaster

    It could happen, yes – as I don’t have a ready made code to test, this needs to be experimented with. With SFTP access I can correct any mistake immediately, while the file manager will not work if I make any mistakes. It would be much safer to use direct access.

    Do you have a test or staging environment by any chance? I would much rather do index code experiments on a non-live site.

    #53207
    erikaerika
    Participant

    You cannot access this content.

    #53208
    erikaerika
    Participant

    you can also just paste you public key here

    #53214
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

    #53220
    erikaerika
    Participant

    You public key has been added, please try now

    #53240
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Thank you very much! I have tried, but I get a refused key message: https://i.imgur.com/pwZs7Fe.png

    However, I didn’t need the SFTP access after all, I was able to create and test a custom code snippet that fetches the PDF attachments from the products and indexes them with the products as well. I have applied the custom code to the functions.php file in your theme directory.

    The way it works is, that you can set up a widget to search for products. If there are any PDFs attached to the product, the code will check and will try to find it in the media library. If it’s in the media library, then it will index it’s contents with the product contents. Therefore searching parts from the PDF contents will return the product as the results as well. Just make sure that he attached PDF exists in the Media library as well.

    The code that I applied below, for archive purposes:

    function asp_get_file_id_from_url($file_url) {
        $file_path = ltrim(str_replace(wp_upload_dir()['baseurl'], '', $file_url), '/');
    
        global $wpdb;
        $statement = $wpdb->prepare("SELECT <code>ID</code> FROM <code>wp_posts</code> WHERE guid='%s';",
            $file_url,
            $file_path);
    
        $attachment = $wpdb->get_col($statement);
        if (count($attachment) < 1) {
            return '';
        }
    
        return $attachment[0]; 
    }
    add_filter(
    	'asp_post_content_before_tokenize',
    	function ( $content, $post ) {
    		if ( $post->post_type !== 'product' ) {
    			return $content;
    		}
    		$product = wc_get_product( $post->ID );
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				var_dump($id, $download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$content .= ' ' . $field_value;
    					}
    				}
    			}
    		}
    
    		return $content;
    	},
    	10,
    	2
    );
Viewing 15 posts - 1 through 15 (of 35 total)
  • You must be logged in to reply to this topic.