WordPress教程 文章上传图片时自动添加alt属性 - 汇站网

WordPress教程 文章上传图片时自动添加alt属性

2024-10-07 0 409

前言:

在 WordPress 中,上传图片时通常需要手动为每张图片指定“替代文本”(即图片的 alt 属性),以提高网站的可访问性和 SEO 效果。然而,这个过程可能会变得繁琐且耗时。为了简化这一流程,可以通过以下代码实现在上传图片时自动将图片的文件名填充到 alt 文本中。此外,此代码还能确保之前上传的图片也会自动更新其 alt 属性,以包含相应的图片名称。
WordPress教程 文章上传图片时自动添加alt属性

将代码添加到当前主题函数模板 functions.php 中:


function dcwd_title_to_words( $title ) {
	// Sanitize the title:  remove hyphens, underscores & extra spaces:
	$title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $title );
 
	// Sanitize the title:  capitalize 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/plugins/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 属性

收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

免责声明

本资源仅用于个人学习和研究使用,禁止用于任何商业环境!

 1.  本网站名称:汇站网
 2.  本站永久网址:https://www.huizhanii.com/
 3.  本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
 4.  本站所有资源的展示图片和信息不代表本站的立场!本站只是储蓄平台及搬运
 5.  下载者禁止在服务器和虚拟机下进行搭建运营,本站所有资源不支持联网运行!只允许调试,参考和研究!!!!
 6.  未经原版权作者许可,禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
 7.  为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
 8.  若资源侵犯了您的合法权益, 请持您的版权证书和相关原作品信息来信通知我们请来信     通知我们 我们会及时删除,给您带来的不便,我们深表歉意!
 9.  如下载链接失效、广告或者压缩包问题请联系站长处理!
 10.  如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
 11.  本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
 12.  因源码具有可复制性,一经赞助 ,不得以任何形式退款。
 13.  更多详情请点击查看

汇站网 WordPress教程 WordPress教程 文章上传图片时自动添加alt属性 https://www.huizhanii.com/38430.html

汇站

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

发表评论
暂无评论
  随机评论 表情开关按钮图片
表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情 表情表情表情表情表情表情表情 表情表情表情表情表情表情表情表情
登录后评论
联系官方客服

为您解决烦忧 - 24小时在线 专业服务

(汇站网)一个专注站长资源的平台网站,提供最新的网站模板和整站源码,内容包含各类精品网页模板,企业网站模板,网站模板,DIV+CSS模板,织梦模板,帝国cms模板,discuz模板,wordpress模板,个人博客论坛模板,上千种免费网页模板下载尽在汇站网.找源码上汇站.huizhanii.com