Skip to content

Commit d41c604

Browse files
ivanblagdanadamziel
andauthoredSep 24, 2024··
Rewriting media upload requests (#63)
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 <[email protected]>
1 parent 548cc25 commit d41c604

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3+
"meta": {
4+
"title": "Serve media files from another host",
5+
"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.",
6+
"author": "ivanblagdan",
7+
"categories": ["Settings"]
8+
},
9+
"landingPage": "/category/uncategorized/",
10+
"constants": {
11+
"PLAYGROUND_REDIRECT_UPLOADS_URL": "https://production.example.com"
12+
},
13+
"steps": [
14+
{
15+
"step": "writeFile",
16+
"path": "/wordpress/wp-content/mu-plugins/redirect-uploads.php",
17+
"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; } });"
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.