在发布缩略图wordpress时强制使用alt属性

在发布缩略图wordpress时强制使用alt属性,wordpress,thumbnails,Wordpress,Thumbnails,当管理员在wordpress上添加新缩略图时,我想强制缩略图上的属性“alt” 如果在输入“title”上保存缩略图验证器时有javascript钩子,那就太好了 谢谢。找到这个,将它放在主题目录中的functions.php文件中 function add_alt_tags($content) { global $post; preg_match_all('/<img (.*?)\/>/', $content, $images); if(!is_null($

当管理员在wordpress上添加新缩略图时,我想强制缩略图上的属性“alt”

如果在输入“title”上保存缩略图验证器时有javascript钩子,那就太好了


谢谢。

找到这个,将它放在主题目录中的functions.php文件中

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);
函数添加标签($content)
{
全球$员额;
preg_match_all(“//”,$content,$images);
如果(!为null($images))
{
foreach($images[1]作为$index=>$value)
{
如果(!preg_match('/alt=/',$value))
{

$new\u img=str\u replace(“找到这个,把它放在主题目录下的functions.php文件中

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);
函数添加标签($content)
{
全球$员额;
preg_match_all(“//”,$content,$images);
如果(!为null($images))
{
foreach($images[1]作为$index=>$value)
{
如果(!preg_match('/alt=/',$value))
{

$new_img=str_replace(“默认情况下,返回的图像没有标题或alt属性。(自WordPress 4.7以来,alt属性不再自动添加。只有在上载图像时特别输入“alt text”,或者返回媒体库并为图像输入“alt text”,它才会具有alt属性)

目前,如果你的图片都有(title属性标签)和(alt标签),那么来自谷歌搜索的网站流量要高得多。因此,我使用主题“functions.php”中的以下函数添加title和alt属性来发布缩略图title和alt属性的值将取自图像的标题,即附件的标题(而不是实际的文章标题)

如果不希望图像属性取自默认图像名称 您可以更改代码,以便从中获取图像属性 “职位名称”如下:


希望这能帮助您节省时间,祝您度过愉快的一天:)

默认情况下,返回的图像没有标题或alt属性。(自WordPress 4.7以来,alt属性不再自动添加。如果您专门输入“alt text”,它将只具有alt属性上载图像时,或者如果您返回媒体库并为图像输入“Alt text”(替换文本)

目前,如果你的图片都有(title属性标签)和(alt标签),那么来自谷歌搜索的网站流量要高得多。因此,我使用主题“functions.php”中的以下函数添加title和alt属性来发布缩略图title和alt属性的值将取自图像的标题,即附件的标题(而不是实际的文章标题)

如果不希望图像属性取自默认图像名称 您可以更改代码,以便从中获取图像属性 “职位名称”如下:

希望这有助于您节省时间,祝您度过愉快的一天:)

// Force adding missing image alt & title for WordPress.
function eln_add_img_title( $attr, $attachment = null ) {

$img_title = trim( strip_tags( $attachment->post_title ) );

$attr['title'] = the_title_attribute( 'echo=0' );
$attr['alt'] = the_title_attribute( 'echo=0' );

return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2     );