仅更改图像源url中的站点url


Change only the site url in image source url

如何将图像源url中的站点url更改为新的站点url,例如:

原图片:http://domain.com/theme/wp-content/uploads/2014/12/image.jpg

替换后:http://new-domain.com/new-theme/wp-content/uploads/2014/12/image.jpg

我如何使用PHP做到这一点?

我使用str_replace
http://php.net/str_replace

$old_src = 'http://domain.com/theme/wp-content/uploads/2014/12/image.jpg';
$old_url = 'http://domain.com/theme/';
$new_url = 'http://new-domain.com/new-theme/';
$new_src = str_replace($old_url, $new_url, $old_src ).'<br/>';
echo $new_src;

进行此更改的最简单方法是在数据库级别。

当我部署一个站点并且需要将暂存域(staging.test.com)更改为活动域(test.com)时,我一直在做这种事情。一般来说,Wordpress通常使用wp-config.php中设置的WP_SITEURL来确定资源路径,但对于图像来说不是这样。

我使用搜索和替换Wordpress数据库脚本,它非常可靠,只需替换:

 http://domain.com/theme/

 http://new-domain.com/new-theme/

你应该可以走了。

或者,如果您不移动站点,那么您可以更改数据库中的特定实例-为此,我建议使用像Navicat(用于OSX)这样的工具,因为它可以在特定表上执行搜索/替换操作。