Urgent.News

What's breaking now, across thousands of outlets.

Tech

How I Added Page-Level Deep Linking to a WordPress PDF Search Plugin

Search found the right PDF. It just never landed anywhere near the actual answer. Here's how I fixed that. A support message summed up the problem better than I could: search finds the right PDF, but the link always opens page one, even when the match is on page 84. Technically, the search worked. Practically, the person still had to do the work themselves once the file opened. This post walks…

A WordPress PDF search plugin was initially unable to accurately link directly to the specific page where a search term appeared. This issue was troubling users who had to manually navigate to the desired page after opening the PDF. The solution involved modifying the database schema and search functionality to store and retrieve page-level information.

The first step was to split the original table into two separate tables: wp_pdf_search_files and wp_pdf_search_pages. This change allowed each page of a PDF to be stored as an individual entry, retaining the page number and text content. The indexing process was then altered to insert each page into the wp_pdf_search_pages table instead of concatenating all pages into a single string.

The second step focused on determining the most relevant page match for a given search term. A new function, search_pdf_pages, was created to retrieve the top matching page for each file based on relevance. This function compares the relevance scores of all matching pages and selects the one with the highest relevance for each file.

Finally, the last step involved generating a working link to the specific page within the PDF. The pdf_page_url function takes the attachment_id and page_number as parameters and constructs a URL that includes a fragment identifier (#page=) to open the PDF at the correct page. By implementing these changes, the plugin now provides users with precise page-level deep linking, enhancing their experience by directly accessing the relevant content within a PDF document.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Wednesday 16 September →