change file url in wordpress slug method doesn't work

How to Change File URL in WordPress: 2 Working Methods

Change file URL in WordPress is something the dashboard slug option cannot actually do. Editing the slug only updates the attachment page, not the actual file URL on your server. To change file URL in WordPress and update the address that gets shared, embedded, and indexed by search engines, you have 2 options: edit your theme’s functions.php file with code, or use the Prevent Direct Access plugin without writing code.

Here’s why this matters and how each method works.

 

Why editing the slug doesn’t change file URL in WordPress

Editing the slug in the WordPress Media Library only changes the attachment page URL, not the file URL itself. That means the actual file on your server keeps the same name and the same address. In other words, the slug controls a secondary page that WordPress generates around the file, while the file URL points to the physical file in your uploads folder.

Here’s why this is important: when you embed a media file in a post or share its link, WordPress uses the file URL, not the attachment page. If you want a clean, SEO-friendly URL for the file you actually share, you need to change file URL in WordPress at the file level.

📝 Method 1: Change file URL in WordPress with code

If you are comfortable working with code, you can edit your theme’s functions.php file to change media file URLs. This method uses the wp_get_attachment_url filter to modify the URL WordPress generates for any media file. Back up your site before making any changes, since a wrong edit can break your site.

Here’s how it works:

  • Open an FTP client or your hosting file manager and locate the functions.php file of your active WordPress theme.
  • Right-click the functions.php file and select Edit to open it in the editor.
  • Add a custom filter to hook into the wp_get_attachment_url function. This tells WordPress to run your code every time it generates a media file URL.
  • Define a function that returns your new URL with the changes you want.
  • Save the file and you’re done.

That’s how you change file URL in WordPress with code. For technical reference, you can review the official wp_get_attachment_url filter reference on the WordPress developer site.

🔌 Method 2: Change file URL in WordPress without code

If you prefer not to touch code, the Prevent Direct Access Gold plugin lets you change file URL in WordPress from the Media Library. The plugin protects your original media files and creates custom private download links with the URL structure you choose. That means you can update internal links automatically without rewriting them one by one.

Here’s how to do it:

  • Install and activate both the free Prevent Direct Access plugin and the Gold version.
  • Go to your Media Library and select the file you want to edit.
  • Click Configure file protection, then Protect this file.
  • Click Customize new link.
  • Fill in your custom download URL slug, download limit, and download expiry.
  • Click Submit.

Your custom URL is live, the original file stays protected, and the internal links on your site keep working. This is the simplest way to change file URL in WordPress without touching code. For more details on file protection options, check the PDA Gold documentation.

Which method should you choose to change file URL in WordPress?

To change file URL in WordPress, choose the code method if you are comfortable editing PHP files and managing backups. It gives you full control but requires technical knowledge. Choose the plugin method if you want to protect your WordPress media files without writing code, and you also want clean URLs at the same time. Both methods work, but each fits a different workflow.

❓ Frequently Asked Questions

Does editing the slug change file URL in WordPress?

No. Editing the slug only changes the attachment page URL, not the file URL on your server. The file keeps its original name in the uploads folder.

What is the difference between the file URL and the permalink in WordPress?

The file URL points to the physical media file on your server (for example, /wp-content/uploads/2026/04/image.jpg). The permalink points to the attachment page that WordPress generates around the file. Each can be edited independently.

Will changing the file URL break my existing links?

If you change the file URL with code, you must update existing references manually or the old links will return 404 errors. Using PDA Gold avoids this problem because the plugin keeps internal links working automatically.

Do I need both the free and Gold versions of Prevent Direct Access?

Yes. The free Prevent Direct Access plugin is required as the base, and the Gold version adds the URL customization features.

Is editing functions.php safe?

Editing PHP files can break your site if there is a syntax error. Always back up your site before making changes, and test on a staging environment first when possible.

Video Transcript

How to change file URL in WordPress.

WordPress doesn’t let you change the file URL of a media file from the dashboard. The slug option only changes the attachment page, not the file itself. To actually change the file URL, you have 2 options: edit your theme’s functions.php file, or use the Prevent Direct Access plugin.

Here’s how each one works.

Method 1. If you know code, you can edit the functions.php file. Back up your site first, a wrong edit can break it.

Step 1. Open an FTP client or your hosting file manager, and locate the functions.php file of your WordPress theme.

Step 2. Right-click the functions.php file and select Edit to open it.

Step 3. Add a custom filter to hook into the wp_get_attachment_url function. This tells WordPress to run your code every time it generates a media file URL.

Step 4. Define a function that returns your new URL with the changes you want.

Step 5. Save the file and you’re done.

Method 2. Prevent Direct Access Gold protects your media files and changes their URLs, no code needed.

Step 1. Install and activate both the free and Gold version.

Step 2. Go to your Media Library and select the file you want to edit.

Step 3. Click Configure file protection, then Protect this file.

Step 4. Click Customize new link.

Step 5. Fill in your custom download URL slug, download limit, and download expiry.

Step 6. Click Submit.

Your custom URL is live, the original file stays protected, and the internal links on your site keep working.

Subscribe for more WordPress security tutorials.