Nâng cấp plugin Slim SEO 4.11.5

Vấn đề

Plugin Slim SEO phiên bản 4.11.5 thiếu tính năng tạo ra thẻ meta og:image:type cho giao thức Open Graph để khai báo định dạng MIME (MIME Type) của hình ảnh đại diện, giống như:

<meta property="og:image:type" content="image/jpeg" />

Cách giải quyết

#1. Tôi thêm biến $img_type và 3 dòng code vào hàm get_data( $id ) trong tập tin slim-seo/src/MetaTags/Image.php, kết quả như dưới đây:

	private function get_data( $id ): array {
		$image = wp_get_attachment_image_src( $id, 'full' );
$img_meta = get_post( $id );
$img_type = $img_meta->post_mime_type;
		return $image ? [
			'id'     => $id,
			'src'    => $image[0],
			'width'  => $image[1],
			'height' => $image[2],
'type' => $img_type,
			'alt'    => get_post_meta( $id, '_wp_attachment_image_alt', true ) ?: get_the_title( $id ),
		] : [];
	}

Nâng cấp plugin Slim SEO

#2. Viết thêm hàm get_image_type() và thêm thuộc tính og:image:type vào hàm output() trong tập tin slim-seo/src/MetaTags/OpenGraph.php, kết quả như dưới đây:

public function output(): void {
		$properties = apply_filters( 'slim_seo_open_graph_tags', [
			'og:title',
			'og:type',
			'og:image',
			'og:image:width',
			'og:image:height',
'og:image:type',
			'og:image:alt',
			'og:description',
			'og:url',
		] );
.....

}
private function get_image_type() {
		return $this->get_image_attribute( 'type' );
}

Trang plugin Slim SEO tại WordPress.org »

Cần lưu ý đây là một bài viết thiên về code PHP trên WordPress, tôi không dùng và không có ý khuyến nghị bạn sử dụng plugin này.