Redirect directly accessed images to WordPress template file and retrieve attachment
4045
€50(approx. $53)
- Posted:
- Proposals: 0
- Remote
- #219581
- Archived
Description
Experience Level: Intermediate
The task for this job consists of a htaccess snippet and php template.
Problem case:
When a user attempts to load an image directly into the browser (for example coming from Google Images or by any other means), the browser normally opens just the bare image, without any reference to the WordPress website where that image was originally uploaded to.
Situation example:
Let’s assume the following example with Google Images as a storyboard to better understand how I would like to have this sorted out.
1) User is browsing Google Images and finds an image.
2) Google Images offers the user to open the website where the image was found or just open the original image.
3) The user chooses the latter, therefore the ball is now on the webserver where the image is hosted.
4) htaccess detects a human operated browser trying to access directly to an image in /uploads/ folder (remember, this could not be inside wp-content, but a differently named directory).
5) htaccess redirects the user to a script inside WordPress theme, however passing the original requested URL in the post value (example http://www.mydomain.com/?image=http://www.mydomain.com/wp-content/uploads/2013/1/my-image.jpg).
6) Let’s assume that the script is located in single-attachment.php; this WordPress template file normally processes attachments, but in this case takes the URL originally requested and uses url_to_postid() function to retrieve the original post where that image was attached to. If no post attachment is found, redirects to homepage.
7) However, if the request comes from a bot (ie Google Images crawler) the images are not redirected and can be accessed directly, therefore all of the above is not exectued.
What I’m looking for is:
a) some .htaccess snippet to redirect requests from human browsers who try to access images drectly – jpg, jpeg, png, gif… – in the Uploads folder directly (important: mind that the uploads might be in a different folder than wp-content) to a wordpress template file (I was thinking ‘single-attachment.php’ and use some conditional php statement – if… else – in case there’s not a wp query running but a redirect from htaccess).
b) a snippet for the template file which would get the post value from the URL to fetch the original URL requested (to the image file) and use that to retrieve the original post where the image could have been attached to. Finally, redirects the user to this post.
Naturally this shouldn’t work only for google images, the imporant thing would be to tell the difference between a bot and a human operated browser.
Problem case:
When a user attempts to load an image directly into the browser (for example coming from Google Images or by any other means), the browser normally opens just the bare image, without any reference to the WordPress website where that image was originally uploaded to.
Situation example:
Let’s assume the following example with Google Images as a storyboard to better understand how I would like to have this sorted out.
1) User is browsing Google Images and finds an image.
2) Google Images offers the user to open the website where the image was found or just open the original image.
3) The user chooses the latter, therefore the ball is now on the webserver where the image is hosted.
4) htaccess detects a human operated browser trying to access directly to an image in /uploads/ folder (remember, this could not be inside wp-content, but a differently named directory).
5) htaccess redirects the user to a script inside WordPress theme, however passing the original requested URL in the post value (example http://www.mydomain.com/?image=http://www.mydomain.com/wp-content/uploads/2013/1/my-image.jpg).
6) Let’s assume that the script is located in single-attachment.php; this WordPress template file normally processes attachments, but in this case takes the URL originally requested and uses url_to_postid() function to retrieve the original post where that image was attached to. If no post attachment is found, redirects to homepage.
7) However, if the request comes from a bot (ie Google Images crawler) the images are not redirected and can be accessed directly, therefore all of the above is not exectued.
What I’m looking for is:
a) some .htaccess snippet to redirect requests from human browsers who try to access images drectly – jpg, jpeg, png, gif… – in the Uploads folder directly (important: mind that the uploads might be in a different folder than wp-content) to a wordpress template file (I was thinking ‘single-attachment.php’ and use some conditional php statement – if… else – in case there’s not a wp query running but a redirect from htaccess).
b) a snippet for the template file which would get the post value from the URL to fetch the original URL requested (to the image file) and use that to retrieve the original post where the image could have been attached to. Finally, redirects the user to this post.
Naturally this shouldn’t work only for google images, the imporant thing would be to tell the difference between a bot and a human operated browser.
Fulvio N.
0% (0)Projects Completed
-
Freelancers worked with
-
Projects awarded
0%
Last project
19 Apr 2024
Malaysia
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
There are no clarification messages.
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies