【问题标题】:Change only the site url in image source url仅更改图像源 url 中的站点 url
【发布时间】:2015-05-25 21:57:40
【问题描述】:

如何将图像源 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 做到这一点?

【问题讨论】:

标签: php wordpress


【解决方案1】:

我通过使用 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;

【讨论】:

    【解决方案2】:

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

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

    我使用Search and Replace for Wordpress databases 脚本,它非常可靠,只需替换:

     http://domain.com/theme/
    

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

    你应该很高兴......

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

    【讨论】:

    • 但是我想在主题设置期间做这个动作,我不需要这样做。我需要类似 (explode, preg_match, str_replace)
    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 2010-11-01
    • 2016-11-24
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 2023-03-22
    • 2017-07-20
    相关资源
    最近更新 更多