优化WordPress图片的SEO自动加上 title 和 alt 标签

基于SEO的考虑,我们需要在WordPress内容的图片中添加上TITLE和ALT标签,提高图片的SEO。有些时候我们会自动添加,但是大部分时候是会忘记的,这里我们直接给容易忘记的朋友设置自动添加。


//图片img标签添加alt,title属性
function imagesalt($content) {
       global $post;
       $pattern ="/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<img$1src=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
add_filter('the_content', 'imagesalt');
//图片A标签添加alt,title属性
function aimagesalt($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
add_filter('the_content', 'aimagesalt');

 

投上你的一票

【AD】丽萨主机英国VPS推荐 英国原生IP双ISP家宅主机

【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工