Get WPFTS Pro today with 25% discount!

How to implement FullTextSearch on a page?


  • @EpsilonAdmin Ok, so after my last reply, I found this post about the Astra theme not showing Smart excerpts:

    [https://docs.fulltextsearch.org/forum/topic/92/solved-the-astra-theme-does-not-show-smart-excerpt-for-files?_=1664945965846](link url)

    So I implemented the solution mentioned in that post, and it helped a lot (but still missing SOME information). To see the current status, go to my Document Search page here:

    https://staging4.hamlinny.org/document-center/

    and enter "Program Notice" in the search field (without the quotes), and click the Search button. You will now see this search results page:

    [https://staging4.hamlinny.org/?wpfts_wdgt=preset_dlp&s=Program+Notice](link url)

    You can see that now there is an excerpt shown, along with highlighted references to the search term, and also a Score.

    However, is there any way to also get the Article Author/Date and a Download link, like that shown on the previously mentioned site? Shown here:

    https://searchannavonreitz.americanstatenationals.org/?s=consent&numberposts=30&results_hide_fields=

    I know that the document names that show up on the search results page are hyperlinked to the document, but it would still be nice to get the Download link to appear, like on the previously mentioned site, and also the document date. If that is not possible, is it at least possible to get the hyperlinked document title to open in a new tab?

    One last thing: If you enter nothing in the search field, then click the Search button, nothing should happen. Instead, it takes you to the search results page and shows many pages of document titles. Can this be fixed?

    Thank you so much!


  • Hi, Larry

    The Date and Author is not related to WPFTS, it's the theme author preferences - whether include Date and Author info to the post list. However, thanks to WP's hook system we can add this functionality with a simple code.

    I would recommend to place it into the child theme's functions.php, however you can put it into the main theme functions.php if you are not using child theme.

    add_action('astra_entry_content_before', function()
    {
    	if (is_search()) {
    		// Modify search result posts
    		// Add date and author
    		$author = get_the_author();
    		$author_url = get_the_author_link();
    		
    		echo '<span class="post-meta-infos"><time class="date-container minor-meta updated" itemprop="datePublished">'.the_date().'</time><span class="text-sep"> / </span><span class="blog-author minor-meta">by <span class="entry-author-link" itemprop="author"><span class="author"><span class="fn"><a href="'.htmlspecialchars($author_url).'" title="Posts by '.htmlspecialchars($author).'" rel="author">'.htmlspecialchars($author).'</a></span></span></span></span></span>';
    	}
    });
    

    This code will add Date and Author info to search results.


  • @EpsilonAdmin Thank you!

    On the search results page, when clicking on the titles to open a document, is there a way to make that open in a new browser tab, rather than in the same tab?

    Thanks again.


  • Hi, Larry

    Yes, we can use Astra-specific hook to add target="_blank" attribute to the title's link.

    Also I made a hook that adds "Download" link below near to "score" value. Unfortunately it's hard to detect filesize at this point (since the file can be located on another server).

    Place this code right below the code that I posted above, in the functions.php:

    
    // Hook to force search result files to open in new tab
    add_filter('astra_the_post_title_before', function($before)
    {
    	if (is_search()) {
    		// Modify the search result title
    		$before = preg_replace('/<a\s+href=/', '<a target="_blank" href=', $before);
    	}
    	
    	return $before;
    });
    
    // Hook to add "Download" link below the excerpt (near the "Score" value)
    add_filter('wpfts_se_output', function($a, $post)
    {
    	if ($a && $post) {
    		if (isset($post['post_type']) && ($post['post_type'] == 'dlp_document')) {
    			// Add Download button
    			$shift = (strlen($a['score']) > 0) ? ' wpfts-shift' : '';
    
    			$link = '';
    
    			$post_id = $post['ID'];
    			
    			$link_type = get_post_meta($post_id, '_dlp_document_link_type', true);
    			$file_size = get_post_meta($post_id, '_dlp_document_file_size', true);
    			$file_id = get_post_meta($post_id, '_dlp_attached_file_id', true);
    			$link_url = get_post_meta($post_id, '_dlp_direct_link_url', true);
    
    			$local_url = false;
    			if ($link_type === 'file') {
    				// The attachment post
    				if ($file_id > 0) {
    				
    					$link = wp_get_attachment_url($file_id);
    				}
    			
    			} elseif ($link_type === 'url') {
    				// The link
    				$link = $link_url;
    			} else {
    				// Unknown link_type
    			
    			}
    			
    			if (strlen($link) > 0) {
    				$a['link'] = '<a target="_blank" class="wpfts-download-link'.$shift.'" href="'.esc_url($link).'"><span>'.__('Download', 'fulltext-search').'</span></a>';
    			}
    		}
    	}
    	
    	return $a;
    }, 10, 2);
    

    Hope this helps. Thanks you!


  • @EpsilonAdmin This works great except for one small error in the constructed download link. For example, here is the constructed link for one of the search results:

    https://https//staging4.hamlinny.org/documents/assessors-office/assessment-rolls/2019/2019-Final-Roll.pdf

    The second occurrence of the "https//" is unnecessary.

    I tried to fix this, but php is not my strong point.

    Larry


  • Okay, Larry,

    could you please show me how the links looks like when added to the Document Library Pro document edit window in WP Admin?
    I just checked on my side and I don't see this problem, so I think the difference is how we put links to documents.

    Thanks!


  • @EpsilonAdmin Attached is a screen cap of one of the documents. The full link for this document is:

    https://staging4.hamlinny.org/documents/assessors-office/assessment-rolls/2019/2019-Final-Roll.pdf

    If you go to my documents page here:

    [https://staging4.hamlinny.org/document-center/](link url)

    and search for the word "Sewer" (without the quotes), this document will be the first one in the search results. Note that the link for the title is correct, while the URL for the Download link is incorrect.

    edit-document.jpg


  • Okay, Larry,

    please could you go to the code we added last time and replace 6th line from the end:

    $a['link'] = '<a target="_blank" class="wpfts-download-link'.$shift.'" href="'.esc_url($link).'"><span>'.__('Download', 'fulltext-search').'</span></a>';
    

    by this one:

    $a['link'] = '<a target="_blank" class="wpfts-download-link'.$shift.'" href="'.htmlspecialchars($link).'"><span>'.__('Download', 'fulltext-search').'</span></a>';
    

    (actually only esc_url has been replaced by htmlspecialchars).

    I think this function breaks the URL somehow on your server (and it's VERY strange).

    Please let me know. Thanks!


  • @EpsilonAdmin I replaced esc_url with htmlspecialchars, but that didn't fix the issue. Very strange, indeed.

    You can see when you mouseover the Download link that the url is incorrect, right? But the link on the title is correct!


  • @sanfordandson Could there be something in the .htaccess file that is causing this?


  • I don't think so, it looks like a PHP problem.

    Which version of the Document Library Pro you are using?


  • @EpsilonAdmin 1.7.2 (current version)


  • @sanfordandson I know it's a cludge, but could you strip out all occurrences of "https//"? That would make the url correct.


  • Okay, Larry

    I am sure they made some tricks in the plugin to modify URL, but I can't say which tricks, since I only have version 1.1.2 at the moment.

    So let's go with the "patch" solution. Please replace the line

    $a['link'] = '<a target="_blank" class="wpfts-download-link'.$shift.'" href="'.htmlspecialchars($link).'"><span>'.__('Download', 'fulltext-search').'</span></a>';
    

    with this one

    $a['link'] = '<a target="_blank" class="wpfts-download-link'.$shift.'" href="'.htmlspecialchars(preg_replace('~^\/\/http~', 'http', $link)).'"><span>'.__('Download', 'fulltext-search').'</span></a>';
    

    It should work.


  • @EpsilonAdmin Wow, still didn't work. I even deleted the astra theme's functions.php file from the server, refreshed the browser (it blew up, as expected), then uploaded the modified functions.php file. I did all this to make sure there was no cache issue. Still doesn't work. Real bizarre.

    Would it help if I sent you login credentials and ftp credentials via email?


  • Hi, Larry

    This would be great, so I can check by myself.
    BTW I see a strange error in search results page.

    Unfortunately I gonna go right now and will be back in some hours to check WP admin. Please send me access to email.

    Thanks!


  • @EpsilonAdmin Will do.


  • Hi, Larry

    The problem with the links has been fixed - just a typo in the code. The code in the messages above is correct.

Suggested Topics

  • 5
  • 1
  • 2
  • 1
  • 2

Be the first to read the news!

We are always improving our products, adding new functions and fixes. Subscribe now to be the first to get the updates and stay informed about our sales! We are not spammy. Seriously.

Join Us Now!

We are a professional IT-team. Many of us have been working in a Web IT field for more than 10 years. Our advanced experience of software development has been employed in the creation of the WordPress FullText Search plugin. All solutions implemented into the plugin have been used for 5 or more years in over 60 different web-projects.

We are looking forward to your comments, requests and suggestions in relation to the current plugin and future updates.

ewm-logo-450

The forum powered by NodeBB | Contributors