Tự động Thêm Thông Tin Alt Text, Title, Caption, Description

Tự động thêm thông tin Title,Caption,Alt Text,Description khi đăng hình ảnh WordPress

Tự động thêm thông tin Title, Caption, Alt Text, Description khi đăng hình ảnh WordPress giúp bạn có thể tối ưu hóa SEO hình ảnh khi đăng tải upload hình ảnh lên WordPress.

Tự động thêm thông tin Title,Caption,Alt Text,Description khi đăng hình ảnh WordPress  như thế nào ?

Để tự động thêm thông tin Title, Caption, Alt Text, Description khi đăng hình ảnh lên WordPress bạn thực hiện các bước dưới đây:

Bước 1. Bạn vào trang quản trị website WordPress. Sau đó chọn vào Appearance > Chọn Theme File Editor > chọn vào function.php

Bước 2. Chèn đoạn mã bên dưới vào ô nhập liệu

/**
* Auto Add Image Attributes From Image Filename
*
* @author Arun Basil Lal
* @refer https://millionclues.com/?p=3908
* @plugin https://wordpress.org/plugins/auto-image-attributes-from-filename-with-bulk-updater/
*/
function abl_mc_auto_image_attributes( $post_ID ) {
$attachment = get_post( $post_ID );

$attachment_title = $attachment->post_title;
$attachment_title = str_replace( '-', ' ', $attachment_title ); // Hyphen Removal
$attachment_title = ucwords( $attachment_title ); // Capitalize First Word

$uploaded_image = array();
$uploaded_image['ID'] = $post_ID;
$uploaded_image['post_title'] = $attachment_title; // Image Title
$uploaded_image['post_excerpt'] = $attachment_title; // Image Caption
$uploaded_image['post_content'] = $attachment_title; // Image Description

wp_update_post( $uploaded_image );
update_post_meta( $post_ID, '_wp_attachment_image_alt', $attachment_title ); // Image Alt Text
}
add_action( 'add_attachment', 'abl_mc_auto_image_attributes' );

Bước 3. Nhấn Save Change để lưu thay đổi

Kết luận: Tự động thêm thông tin Title,Caption,Alt Text,Description khi đăng hình ảnh WordPress  như thế nào ?

Để tự động thêm thông tin Title,Caption,Alt Text,Description khi đăng hình ảnh WordPress khá đơn giản đúng không các bạn. Hãy thêm đoạn code trên vào file function.php để  khi upload hình ảnh thì hình ảnh sẽ tự động cập nhật thông tin Title, Caption, Alt Text, Description của hình ảnh nhé.

Table of Contents