function add_auto_meta_description() {
if (is_single()) { // Chỉ áp dụng cho bài viết (post)
global $post;
if ($post) {
$content = strip_tags($post->post_content); // Loại bỏ HTML
$content = preg_replace('/\s+/', ' ', $content); // Xóa khoảng trắng thừa
$meta_description = mb_substr($content, 0, 160, 'UTF-8'); // Lấy 160 ký tự đầu tiên
if (!empty($meta_description)) {
echo '<meta name="description" content="' . esc_attr($meta_description) . '">' . "\n";
}
}
}
}
add_action('wp_head', 'add_auto_meta_description', 1);