方法一:通过主题的 functions.php 文件来批量替换
在主题文件的 functions.php(WP后台“外观”-“编辑”右侧可以找到)里加入如下代码(注意域名需要做相应修改)
/* 替换图片链接为 https */ /* 来源半夏小栈(https://www.banxia.me/995.html): */ function https_image_replacer($content){ if( is_ssl() ){ /*已经验证使用 $_SERVER['SERVER_NAME']也可以获取到数据,但是貌似$_SERVER['HTTP_HOST']更好一点*/ $host_name = $_SERVER['HTTP_HOST']; $http_host_name='http://'.$host_name.'/wp-content/uploads'; $https_host_name='https://'.$host_name.'/wp-content/uploads'; $content = str_replace($http_host_name, $https_host_name, $content); } return $content; } add_filter('the_content', 'https_image_replacer');
function replacehttp($content){ if( is_ssl() ){ $content = str_replace('http://域名/wp-content/uploads', 'https://域名/wp-content/uploads', $content); } return $content; } add_filter('the_content', 'replacehttp');
方法二:通过 SQL 语句进行正文的批量替换
UPDATE wp_posts SET post_content = replace(post_content, 'http://域名/wp-content/uploads','https://域名/wp-content/uploads');
评论前必须登录!
注册