I currently have a plugin that pulls posts that I create on my blog using a RSS Feed to WordPress. The problem is its hotlinking my images. I want WordPress to save the images locally, then display them. Displaying the images I have no issue with. This is the best I could find and tweaked it to my liking but it only crashes my website:
$data = my_get_remote_content('get_post_meta( get_the_ID(), 'enclosure', true )');
savePhoto(my_get_remote_content($data['body']), $post->ID);
function my_get_remote_content($url) {
$response = wp_remote_get($url,
array(
'headers' => array(
'user-agent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2)'
)
)
);
if( is_wp_error( $response ) ) {
throw new Exception('Error fetching remote content');
} else {
$data = wp_remote_retrieve_body($response);
return $data;
}
}
function savePhoto($fileContents, $isbn) {
if (DIRECTORY_SEPARATOR=='/'){
$absolute_path = dirname(__FILE__).'/';
} else {
$absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/';
}
$newImg = imagecreatefromstring($fileContents);
return imagejpeg($newImg, $absolute_path ."{$isbn}.jpg",100);
}
get_post_meta( get_the_ID(), 'enclosure', true )
gets the actual link from my post meta data (custom fields) but I can't seem to make it work, can anyone help me out?
Thanks