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

反诈示警:如遇到文章内跳转至别的网站或产生需要交钱,交易等行为,均为诈骗行为,请谨慎对待。对于网上兼职、刷单、刷信誉的就是诈骗,请勿相信!需要提供身份证明、短信验证和短信链接点击都是诈骗, 请不要提供!同时请下载“国家反诈中心”App能自查自检,规避诈骗电话,免遭损失。

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

1、方法1

function image_alttitle( $imgalttitle ){
global $post;
$category = get_the_category();
$flname=$category[0]->cat_name;
$btitle = get_bloginfo();
$imgtitle = $post->post_title;
$imgUrl = \"<imgs[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>\";
if(preg_match_all(\"/$imgUrl/siU\",$imgalttitle,$matches,PREG_SET_ORDER)){
if( !empty($matches) ){
for ($i=0; $i < count($matches); $i++){
$tag = $url = $matches[$i][0];
$j=$i+1;
$judge = \'/title=/\';
preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$altURL = \' alt=\"\'.$imgtitle.\' \'.$flname.\' 第\'.$j.\'张\" title=\"\'.$imgtitle.\' \'.$flname.\' 第\'.$j.\'张-\'.$btitle.\'\" \';
$url = rtrim($url,\'>\');
$url .= $altURL.\'>\';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( \'the_content\',\'image_alttitle\');

2、方法2

function image_alttitle( $imgalttitle ){
global $post;
$category = get_the_category();
$flname=$category[0]->cat_name;
$btitle = get_bloginfo();
$imgtitle = $post->post_title;
$imgUrl = \"<imgs[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>\";
if(preg_match_all(\"/$imgUrl/siU\",$imgalttitle,$matches,PREG_SET_ORDER)){
if( !empty($matches) ){
for ($i=0; $i < count($matches); $i++){
$tag = $url = $matches[$i][0];
$j=$i+1;
$judge = \'/title=/\';
preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$altURL = \' alt=\"\'.$imgtitle.\' \'.$flname.\' 第\'.$j.\'张\" title=\"\'.$imgtitle.\' \'.$flname.\' 第\'.$j.\'张-\'.$btitle.\'\" \';
$url = rtrim($url,\'>\');
$url .= $altURL.\'>\';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( \'the_content\',\'image_alttitle\');

3、方法3

function wpface_image_alt_title($content) {
  global $post;
  $alt_title = $post->post_title;
  preg_match_all(\'/<img(.*?)src=(\'|\")(.*?).(bmp|gif|jpeg|jpg|png)(\'|\")(.*?)>/i\', $content, $matches);
  if($matches) {
    foreach($matches[0] as $val) {
      $place_content = $val;
      //先把空白 alt 和 title 属性清理掉
      $place_content = str_replace(\' alt \', \' \', $place_content);
      $place_content = str_replace(\' \', \'\', $place_content);
      $place_content = str_replace(\' title \', \' \', $place_content);
      $place_content = str_replace(\' title=\"\"\', \'\', $place_content);
      //如果想覆盖原来的 alt 或 title 属性,就把下面两句的注释取消
      //$place_content = preg_replace(\'/ alt=\"(.*?)\"/\', \'\', $place_content);
      //$place_content = preg_replace(\'/ title=\"(.*?)\"/\', \'\', $place_content);
      //判断如果没有 alt 或 title 属性就用文章标题添加
      if(strpos($place_content,\'alt=\')===false) {
        $place_content = str_replace(\"/>\", \"\", $place_content).\' alt=\"\'.$alt_title.\'\"/>\';
      }
      if(strpos($place_content,\'title=\')===false) {
        $place_content = str_replace(\"/>\", \"\", $place_content).\' title=\"\'.$alt_title.\'\"/>\';
      }
      //替换 img 标签
      $content = str_replace($val, $place_content, $content);
    }
  }
  return $content;
}
add_filter(\'the_content\',\'wpface_image_alt_title\');

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

如需 WordPress 优化加速、二次开发、网站维护、企业网站建设托管等服务,点此联系我 | 近期站内热门福利推荐:
文章版权及转载声明

本文地址:https://www.zycang.com/40760.html
文章转载或复制请以超链接形式并注明来源出处。 本文最后更新于:2022-07-31 17:28:57
声明:某些文章或资源具有时效性,若有 错误 或 所需下载资源 已失效,请联系客服QQ:11210980

喜欢就支持一下吧
点赞158赞赏 分享
评论 抢沙发

请登录后发表评论