3个设置WordPress内容自动图片添加ALT和TITLE属性的脚本

麦子 定制开发24字数 2739阅读9分7秒阅读模式

从SEO角度考虑,我们的每篇文章中的图片需要加上ALT和TITLE属性。不过,有些时候我们编辑文章的时候忘记添加属性,那我们有没有办法可以批量自动添加呢?在这篇文章中,我们可以通过可选的办法来通过自动给没有设置ALT和TITLE属性的图片自动添加这篇文章的标题作为2个属性。

1、方法1

  1. function image_alttitle( $imgalttitle ){
  2. global $post;
  3. $category = get_the_category();
  4. $flname=$category[0]->cat_name;
  5. $btitle = get_bloginfo();
  6. $imgtitle = $post->post_title;
  7. $imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
  8. if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
  9. if( !empty($matches) ){
  10. for ($i=0; $i < count($matches); $i++){
  11. $tag = $url = $matches[$i][0];
  12. $j=$i+1;
  13. $judge = '/title=/';
  14. preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
  15. if( count($match) < 1 )
  16. $altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'张" title="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" ';
  17. $url = rtrim($url,'>');
  18. $url .= $altURL.'>';
  19. $imgalttitle = str_replace($tag,$url,$imgalttitle);
  20. }
  21. }
  22. }
  23. return $imgalttitle;
  24. }
  25. add_filter( 'the_content','image_alttitle');

2、方法2

  1. function image_alttitle( $imgalttitle ){
  2. global $post;
  3. $category = get_the_category();
  4. $flname=$category[0]->cat_name;
  5. $btitle = get_bloginfo();
  6. $imgtitle = $post->post_title;
  7. $imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
  8. if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
  9. if( !empty($matches) ){
  10. for ($i=0; $i < count($matches); $i++){
  11. $tag = $url = $matches[$i][0];
  12. $j=$i+1;
  13. $judge = '/title=/';
  14. preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
  15. if( count($match) < 1 )
  16. $altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'张" title="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" ';
  17. $url = rtrim($url,'>');
  18. $url .= $altURL.'>';
  19. $imgalttitle = str_replace($tag,$url,$imgalttitle);
  20. }
  21. }
  22. }
  23. return $imgalttitle;
  24. }
  25. add_filter( 'the_content','image_alttitle');

3、方法3

  1. function wpface_image_alt_title($content) {
  2. global $post;
  3. $alt_title = $post->post_title;
  4. preg_match_all('/<img(.*?)src=(\'|\")(.*?)\.(bmp|gif|jpeg|jpg|png)(\'|\")(.*?)>/i', $content, $matches);
  5. if($matches) {
  6. foreach($matches[0] as $val) {
  7. $place_content = $val;
  8. //先把空白 alt 和 title 属性清理掉
  9. $place_content = str_replace(' alt ', ' ', $place_content);
  10. $place_content = str_replace(' alt=""', '', $place_content);
  11. $place_content = str_replace(' title ', ' ', $place_content);
  12. $place_content = str_replace(' title=""', '', $place_content);
  13. //如果想覆盖原来的 alt 或 title 属性,就把下面两句的注释取消
  14. //$place_content = preg_replace('/ alt="(.*?)"/', '', $place_content);
  15. //$place_content = preg_replace('/ title="(.*?)"/', '', $place_content);
  16. //判断如果没有 alt 或 title 属性就用文章标题添加
  17. if(strpos($place_content,'alt=')===false) {
  18. $place_content = str_replace("/>", "", $place_content).' alt="'.$alt_title.'"/>';
  19. }
  20. if(strpos($place_content,'title=')===false) {
  21. $place_content = str_replace("/>", "", $place_content).' title="'.$alt_title.'"/>';
  22. }
  23. //替换 img 标签
  24. $content = str_replace($val, $place_content, $content);
  25. }
  26. }
  27. return $content;
  28. }
  29. add_filter('the_content','wpface_image_alt_title');

以上方法我们选择一个即可。

参考文章:

1、https://www.itbulu.com/wp-auto-titlealt.html

2、https://www.wpface.com/1058.html

投上你的一票
 
  • 本文由 麦子 发表于 2024年11月21日 14:41:20
  • 转载请务必保留本文链接:https://www.zhujipingjia.com/auto-title.html
  • WordPress自动TITLE