Skip to content

Commit

Permalink
Rewriting media upload requests (#63)
Browse files Browse the repository at this point in the history
I had a situation where I wanted playgrounds to mirror a heavy
production website with media upload attachments. We have a similar
pattern in-house for local dev environments where we reverse proxying
image requests to the prod domain.
The following is a basic example of how I've dealt with the problem by
intercepting and redirecting requests for `/wp-content/uploads*`
requests.

---------

Co-authored-by: Adam Zieliński <adam@adamziel.com>
  • Loading branch information
ivanblagdan and adamziel authored Sep 24, 2024
1 parent 548cc25 commit d41c604
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions blueprints/redirect-upload-requests/blueprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
"meta": {
"title": "Serve media files from another host",
"description": "Redirect any requests to files within the uploads directory to an external host. This is useful when you have a lot of image attachments in your playground but don’t want to include them all in the blueprint.",
"author": "ivanblagdan",
"categories": ["Settings"]
},
"landingPage": "/category/uncategorized/",
"constants": {
"PLAYGROUND_REDIRECT_UPLOADS_URL": "https://production.example.com"
},
"steps": [
{
"step": "writeFile",
"path": "/wordpress/wp-content/mu-plugins/redirect-uploads.php",
"data": "<?php add_action('init', function() { if (!defined('PLAYGROUND_REDIRECT_UPLOADS_URL')) exit; $request_uri = $_SERVER['REQUEST_URI']; $uploads_position = strpos($request_uri, '/wp-content/uploads'); if ($uploads_position !== false) { $uploads_path = substr($request_uri, $uploads_position); $redirect_url = PLAYGROUND_REDIRECT_UPLOADS_URL . $uploads_path; wp_redirect($redirect_url, 302); exit; } });"
}
]
}

0 comments on commit d41c604

Please sign in to comment.