应用场景:很多博友的谷歌广告 Google AdSense 申请不通过,提示“网站不符合合作规范”,而大 Google 却不指出哪些地方不符合规范,这样对于我们广大的站长盆友来说就很蛋疼了,一头雾水。有盆友就提出可能是文章内的图片侵权了,我们大多数博文的图片都是来源于网络,而谷歌对图片版权很重视,所以这个原因是很有可能的。最后,机智的盆友马上就想到了解决思路,将所有文章中的所有图片替换为指定默认图片或随机图片,以提高谷歌广告联盟申请通过率~
实现办法很简单,只需要将其放入主题的 functions.php 中即可~
- /**
- * WordPress 如何实现文章图片自动替换为指定图片 - 龙笑天下
- * https://www.ilxtx.com/replace-wordpress-postimages-to-certain-picture.html
- * date:2018-3-12 11:09:24
- */
- function lxtx_replace_postimages( $content ){
- $reg = array('#(http://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#','#(https://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#');
- $pic = get_template_directory_uri().'/images/default.jpg'; // 使用主题根目录的 images 文件夹中的 default.jpg 做为默认输出图片
- // 如若想输出随机图片,则取消下面 2 行注释,并在主题根目录的 images 文件夹中创建一个 random 文件夹并放置 10 张随机图片,图片命名为:1.jpg 2.jpg ... 10.jpg
- // $random = mt_rand(1, 10);
- // $pic = get_template_directory_uri().'/images/random/'.$random.'.jpg';
- $content = preg_replace($reg, $pic, $content);
- return $content;
- }
- add_filter('the_content', 'lxtx_replace_postimages');
友情提示:注意看代码里的注释哦!
内容整理自:龙笑天下 - https://www.ilxtx.com/replace-wordpress-postimages-to-certain-picture.html
如若转载,请注明出处:https://www.ozabc.com/jianzhan/332195.html