Các thuộc tính của ảnh như tiêu đề ảnh, mô tả ảnh trong SEO rất là quan trọng. Và đa phần chúng ta thường quên thêm sau khi add 1 ảnh mới. Hôm nay mình sẽ chia sẻ một đoạn function để giúp các bạn có thể điền 1 cách tự động các thuộc tính này khi thêm 1 ảnh mới nhé, thông tin thêm mới sẽ dựa trên tên file ảnh mà bạn đặt nhé.
Các bạn thêm function này vào file functions.php giúp mình

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; // Tiêu đề ảnh $uploaded_image['post_excerpt'] = $attachment_title; // Chi tiết ảnh $uploaded_image['post_content'] = $attachment_title; // Mô tả ảnh 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' );
Như vậy là xong, khi các bạn thêm ảnh mới thì nó sẽ tự động cập nhật tiêu đề, chi tiết và mô tả ảnh nhé.

Chúc các bạn thành công
Nguồn : https://pttuan410.com