Saturday, 15 January 2011

How to redirect WORDPRESS hotlinked image ( for exp : Image.jpg) to post/page where it is? -



How to redirect WORDPRESS hotlinked image ( for exp : Image.jpg) to post/page where it is? -

recently google alter image search , result of traffic click on view original image , send them direct image file.

i have changed .htaccess , stop hot linking , redirect homepage if click on image file.

recently i've tried if possible redirect post or page image is.

by next code

<?php require('../wp-blog-header.php'); $imageurl = strip_tags( $_get['id'] ); if imageurl!== "") { $query = "select id $wpdb->posts post_type = 'attachment'and post_parent > '0' , guid = '$imageurl'"; $linkedimage = $wpdb-get_row($query); $attachmenturl = get_attachment_link($linkedimage->id); if($attachmenturl !== "" && !is_numeric(stripos($attachmenturl, "attachment_id=0"))) { header("http/1.1 302 found"); header("location: ". $attachmenturl); exit; } } $newurl = get_site_url() . "/image-not-found"; header("http/1.0 404 not found"); header("location: " . $newurl); exit; ?>

but redirect http://www.mydomain.com/?attachment_id=

id nil or can't fetch proper id

my website in wordpress cms version 3.5.1

can help how redirect proper attachment page .jpg file direct request.

thanks in advance

your code work if there id parameter on requested url exact match image url, instance http://www.example.com/?id=http://www.example.com/uploads/some-image.jpg

is case? so, shouldn't utilize id since it's reserved parameter other things in wp. in .htaccess, should redirect http://www.example.com/uploads/some-image.jpg to, example, http://www.example.com/?image_redirect=http://www.example.com/uploads/some-image.jpg. utilize $imageurl = $_get['image_redirect'].

other notes: strip_tags doesn't create string safe utilize sql query. in wordpress, have esc_sql($string) that. also, should rather utilize if ( $imageurl != '' ), not !==. , there's error on line 5, should $wpdb->get_row, have minus sign. assume these typos. , finally, shouldn't utilize location: header status code 404.

image wordpress redirect

No comments:

Post a Comment