admin管理员组文章数量:1315832
I would like to generate to generate some kind of automatic alt
attributes for the images, if they don't have alt
set up.
Found this code, but it doesn't work for some reason:
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match('/alt=/', $value))
{
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter('the_content', 'add_alt_tags', 99999);
I would like to generate to generate some kind of automatic alt
attributes for the images, if they don't have alt
set up.
Found this code, but it doesn't work for some reason:
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match('/alt=/', $value))
{
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter('the_content', 'add_alt_tags', 99999);
Share
Improve this question
asked Nov 16, 2020 at 8:44
RunnickRunnick
1,0593 gold badges14 silver badges26 bronze badges
1 Answer
Reset to default 0Maybe not the best solution, but did it like this:
function add_alt_tags($content)
{
global $post;
$content = str_replace('alt=""', 'alt="'.$post->post_title .'"',
$content);
return $content;
}
add_filter('the_content', 'add_alt_tags', 100);
本文标签: imagesHow to generate alt attributes with phpfilters
版权声明:本文标题:images - How to generate alt attributes with phpfilters? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987791a2408786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论