前言:
在 WordPress
中,上传图片时通常需要手动为每张图片指定“替代文本”(即图片的 alt
属性),以提高网站的可访问性和 SEO
效果。然而,这个过程可能会变得繁琐且耗时。为了简化这一流程,可以通过以下代码实现在上传图片时自动将图片的文件名填充到 alt
文本中。此外,此代码还能确保之前上传的图片也会自动更新其 alt
属性,以包含相应的图片名称。
将代码添加到当前主题函数模板 functions
.php
中:
api
talize
first
letter
of
every
word
(other
letters
lower
case
):
$title
= ucwords
( strtolower
( $title
) );
return
$title
;
}
// Copied
from
: https
://brutalbusiness
.com
/automatically
-set
-the
-wordpress
-image
-title
-alt
-text
-other
-meta
/
add_action
( 'add_
attachment
', 'dcwd_set_image_meta_upon_image_upload
' );
function
dcwd_set_image_meta_upon_image_upload
( $post_ID
) {
// Check
if
uploaded
file
is
an
image
, else
do
nothing
if
( wp_attachment_is_image
( $post_ID
) ) {
$my_image_title
= get_post
( $post_ID
)->post_title
;
$my_image_title
= dcwd_title_to_words
( $my_image_title
);
// Create
an
array
with
the
image
meta
(Title
, Caption
, Description
) to
be
updated
// Note
: comment
out
the
Excerpt
/Caption
or
Content
/Description
lines
if
not
needed
$my_image_meta
= array
(
'ID
' => $post_ID
, // Specify
the
image
(ID
) to
be
updated
'post_title
' => $my_image_title
, // Set
image
Title
to
sanitized
title
// Damien
: Omit
setting
the
caption
as
I
rarely
use
captions
when
I
insert
images
.
//'post_excerpt
' => $my_image_title
, // Set
image
Caption
(Excerpt
) to
sanitized
title
'post_content
' => $my_image_title
, // Set
image
Description
(Content
) to
sanitized
title
);
// Set
the
image
Alt
-Text
update_post_meta
( $post_ID
, '_wp_attachment_image_alt
', $my_image_title
);
// Set
the
image
meta
(e
.g
. Title
, Excerpt
, Content
)
wp_update_post
( $my_image_meta
);
}
}
// Enhanced
version
of
: https
://wordpress
.org
/plugin
s
/automatic
-image
-alt
-attributes
/
add_filter
('image_send_to_editor
', 'dcwd_auto_alt_fix_1
', 10, 2);
function
dcwd_auto_alt_fix_1
($html
, $id
) {
$image_title
= get_the_title
( $id
);
$image_title
= dcwd_title_to_words
( $image_title
);
return
str_replace
('alt
=""','alt
="' . $image_title
. '"',$html
);
}
add_filter
('wp_get_attachment_image_attributes
', 'dcwd_auto_alt_fix_2
', 10, 2);
function
dcwd_auto_alt_fix_2
($attributes
, $attachment
){
if
( !isset
( $attributes
['alt
'] ) || '' === $attributes
['alt
'] ) {
$attributes
['alt
'] = dcwd_title_to_words
( get_the_title
( $attachment
->ID
) );
}
return
$attributes
;
}
// From
: https
://mekshq
.com
/change
-image
-alt
-tag
-in
-wordpress
/
/* Replace
alt
attribute
of
post
thumbnail
with
post
title
*/
add_filter
( 'post_thumbnail_html
', 'meks_post_thumbnail_alt_change
', 10, 5 );
function
meks_post_thumbnail_alt_change
( $html
, $post_id
, $post_thumbnail_id
, $size
, $attr
) {
$post_title
= get_the_title
();
$html
= preg_replace
( '/(alt
=")(.*?)(")/i
', '$1'.esc_attr
( $post_title
).'$3', $html
);
return
$html
;
}
这段代码首先定义了一个名为`auto_add_alt_text
`的函数,该函数接受一个图片 ID
作为参数。它获取图片的标题作为 alt
文本,如果标题为空,则使用图片的文件名。然后,它使用`update_post_meta
`函数更新图片的 alt
属性。
接着,通过`add_action
`函数将`auto_add_alt_text
`函数绑定到`add_attachment
`和`edit_attachment
`动作上,确保新上传或编辑的图片都会自动添加 alt
文本。
最后,定义了一个名为`update_existing_images
`的函数,该函数遍历所有已上传的图片,并为它们调用`auto_add_alt_text
`函数,以更新它们的 alt
属性。
通过这种方式,WordPress
网站中的图片 alt
文本将自动填充,从而提高网站的可访问性和搜索引擎优化效果。
转载请注明:汇站网 » WordPress
教程 文章上传图片时自动添加 alt
属性