当前位置:首页>网站建设>WordPress上传图片时自动添加alt属性

WordPress上传图片时自动添加alt属性

释放双眼,带上耳机,听听看~!

默认情况下,WordPress 上传图片需要手动“替代文本”(图片alt属性)添加内容, 可以通过下面的代码上传图片时,自动将图片名称添加到”替代文本”中,之前上传的图片也会自动添加图片名称为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;
}
欢迎访问秀主题博客,分享简单实用WP教程
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

限制WordPress文章标题长度字数多种方法

2024-9-7 10:18:30

网站建设

将title标签作为WordPress文章图片的ALT

2024-9-9 9:05:09

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索