Search & Replace Unprotected File URLs In WordPress Content

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:

  1. Protect these files first
  2. 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

To get started, go to our plugin settings page and simply enable the Search & Replace option.

Here you can only select pages or posts by default. To search and replace the unprotected links in a custom post type, you need to use some custom codes.

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 <img> and <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 _pda into 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.

In short,

  • 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.

Troubleshooting

If the file URLs are not updated even after enabling Search & Replace option, it might be due to one of the following reasons.

  • You’re using a caching plugin or enabling server cache.
    • Solution: Exclude the pages containing your protected files from the cache.
  • You’ve set up SSL/HTTPS incorrectly.
    It usually happens when you move WordPress from HTTP to HTTPS and install an SSL certificate but haven’t updated your site URL yet.

    • Solution: Navigate to Settings >> General page and update both your WordPress and site URL address fields by replacing http with https.

  • Your file URLs don’t match your site URL
    It usually happens when you already embedded files into content before changing your site URL.

How to Select the Entire Post Types

In case you want our plugin to search & replace the entire post type, instead of selecting all existing posts, you can add this code to your (child) theme functions.php.

add_filter( 'pda_before_the_content', 'pda_allow_all_posts_can_search_replace', 300, 2 );
add_filter( 'pda_the_content_sr_pre_condition', 'pda_the_content_sr_pre_condition_callback', 300, 1 );

function pda_the_content_sr_pre_condition_callback( $in_the_loop ) {
    return true;
}

function pda_allow_all_posts_can_search_replace( $conditions, $posts ) {
    if ( ! isset( $conditions['pda_is_using_search_replace'] ) ) {
        return $conditions;
    }
    if ( ! isset( $posts['post_id'] ) ) {
        return $conditions;
    }
    $post_id         = $posts['post_id'];
    $post_type       = get_post_type( $post_id );
    $supported_types = [ 'page', 'post', 'portfolio', 'product' ]; //Post type slugs
    if ( ! in_array( $post_type, $supported_types, true ) ) {
        return $conditions;
    }
    $conditions['pda_is_using_search_replace'] = true;  
    return $conditions;
}

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.

Please make sure you back up your database first before using this method.

1. Using Better Search Replace Plugin

After installing Better Search Replace, go to its settings and perform the following Search & Replace:

  1. Search for original links under a particular folder, e.g.
    https://pda.com/wp-content/uploads/2020/02/
  2. Replace with the PDA protected links, e.g.
    https://pda.com/wp-content/uploads/_pda/2020/02/
  3. Select wp_posts table
  4. 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.

Lasted updated on May 23, 2024