PHP实现图片防盗链
方法三:使用防盗链脚本保护WordPress网站上的图片
function protect_images($content) {
$pattern = '//i';
preg_match_all($pattern, $content, $matches);
foreach ($matches[1] as $image_url) {
$image_path = str_replace(get_site_url(), ABSPATH, $image_url);
$protected_url = get_site_url() . '/protect_image.php?path=' . $image_path;
$content = str_replace($image_url, $protected_url, $content);
}
return $content;
}
add_filter('the_content', 'protect_images');