Once a file is protected by Prevent Direct Access (PDA) Gold, its URL is actually changed as we move the file into our protected folder, i.e. _pda. So if you protect media files already embedded in your content, those images or documents (URL) will go missing.
What you can do is simply updating the unprotected with protected URLs manually in your content. That works if there are only a few protected files on your content. What if there are hundreds of these files?
That’s when our Search & Replace (S&R) settings option comes in useful.
If you haven’t embedded your files into content yet, you should:
- Protect these files first
- Embed them into content as normal
- The protected file URLs are updated for you automatically
- You don’t have to use the S&R feature at all
Search & Replace methods:
Using PDA Gold Search & Replace
Simply go to our plugin settings and enable this option:
Once, the settings option is enabled, our PDA Gold will:
- Filter the content of your WordPress page or post after it’s retrieved from the database and before it’s displayed to the screen using the_content filter
- Search for all the
<a>tag in the content using regular expressions
- Once we have all the file URL of images and links embedded in the content, we will check if the URL exists in the database (DB):
- If it does, the file is not protected and we leave it as it is
- If it doesn’t, we insert the
_pdainto the URL and check again whether it exists in the DB. If it does – meaning the file is protected, we will update its file URL accordingly.
By using WordPress
the_content filter, our Gold version will do the URL replacement automatically for you. In other words, you don’t have to manually update the protected file URL in your content anymore. This method is arguably better than replacing the actual file URLs in the DB which require you to do the reverse process when unprotecting the files.
Performance-wise, it’s recommended to select only pages you want our plugin to filter and replace unprotected URLs in your content.
|Using the_content filter||Replacing the actual file URL in the DB|
|Process||Run and update the file URL dynamically on page load||Run a heavy SQL searching, checking and replacing the file URL of all your attachment files|
|Frequency||On every page load||Once|
|Effectiveness||Run on selected pages only||Have to search and replace the file URL again once the file is unprotected|
This is also how our magic links work as well.
- If there are only a few unprotected file URLs in your content, just manually update them with the protected ones
- If there are hundreds of them, enable our settings option above and select which page(s) our PDA Gold should find and change file URLs automatically for you
How to Perform Search and Replace in WordPress Database
When there are hundreds of pages with many existing media files already embedded on, it’s better to do an actual Search & Replace directly in the WordPress database. There are 2 ways to do so.
1. Using Better Search Replace Plugin
After installing Better Search Replace, go to its settings and perform the following Search & Replace:
- Search for original links under a particular folder, e.g.
- Replace with the PDA protected links, e.g.
- Run Search/Replace
2. Using MySQL Query
You can select a specific post, instead of your entire content (wp_posts), to perform a search and replace.
To do so, access your website database under phpMyAdmin and run this query:
update wp_posts set post_content = replace(post_content,'https://pda.com/wp-content/uploads/2020/02/','https://pda.com/wp-content/uploads/_pda/2020/02/') where post_id = XXX;
Direct Database Search & Replace Limitation
The biggest limitation of this method is they will search & replace both your protected and unprotected files on the same content (or folder). In other words, if there are unprotected files and images, together with the protected, on the same page content, their file URLs will get replaced wrongly as well.
Due to this MySQL regexp_replace bug, you cannot perform a search & replace only for protected file links at the moment.
The overcome this limitation, the search & replace should be done on the server (plugin) instead. Please vote and get this feature to be implemented soonest.