Nếu bạn không muốn sử dụng plugin SEO, bài viết này sẽ hướng dẫn bạn tự động xuất các thẻ Meta và Open Graph đúng theo từng loại trang. Nếu bạn có ý định sử dụng plugin SEO, hãy bỏ qua bước này.

Theme WordPress: Tự động xuất Meta và Open Graph

Cập nhật functions.php

Thêm hàm init_meta vào cuối tập tin functions.php với nội dung như sau.

/**
 * Meta
 */
function init_meta() {
    $options = get_option('init_theme_options');
    if (is_home()) { ?>
<meta name="description" content="<?php echo $options['metadescription']; ?>">
<meta name="keywords" content="<?php echo $options['metakeywords']; ?>">
<meta property="og:url" content="<?php bloginfo('url'); ?>">
<meta property="og:title" content="<?php bloginfo('name'); ?> – <?php bloginfo('description'); ?>">
<meta property="og:description" content="<?php echo $options['metadescription']; ?>">
<meta property="og:type" content="website">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php bloginfo('name'); ?> – <?php bloginfo('description'); ?>">
<meta name="twitter:description" content="<?php echo $options['metadescription']; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
    if (is_category()) {
        $cat_title = single_cat_title('', false);
        $cat_name = mb_strtolower($cat_title); $cat_name = str_replace('"', '', $cat_name); $cat_name = str_replace('“', '', $cat_name); $cat_name = str_replace(',', '', $cat_name); $cat_name = str_replace('!', '', $cat_name); $cat_name = str_replace('”', '', $cat_name);
        $category = get_term_by('name', $cat_title, 'category');
        $cat_link = get_category_link($category->term_id);
        $cat_desc = wp_trim_words(category_description(), 25, '...');
        if (!$cat_desc) $cat_desc = $options['metadescription'];
?>
<meta name="description" content="<?php echo $cat_desc; ?>">
<meta name="keywords" content="<?php echo $cat_name . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $cat_link; ?>">
<meta property="og:title" content="<?php echo $cat_title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $cat_desc; ?>">
<meta property="og:type" content="object">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $cat_title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $cat_desc; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
    if (is_tag()) {
        $tag_title = single_tag_title('', false);
        $tag_desc = 'Tag: ' . $tag_title . ' – ' . $options['metadescription'];
        $tag = get_term_by('name', $tag_title, 'post_tag');
        $tag_link = get_tag_link($tag->term_id);
?>
<meta name="description" content="<?php echo $tag_desc; ?>">
<meta name="keywords" content="<?php echo $tag_title . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $tag_link; ?>">
<meta property="og:title" content="<?php echo $tag_title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $tag_desc; ?>">
<meta property="og:type" content="object">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $tag_title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $tag_desc; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
    if (is_month()) {
        $month_title = trim(single_month_title(' ', false));
        $month_name = trim(strtolower($month_title));
        $month_desc = 'Archive: ' . $month_title . ' – ' . $options['metadescription'];
        $archive_year  = get_the_time('Y');
        $archive_month = get_the_time('m');
        $month_link = get_month_link($archive_year, $archive_month);
?>
<meta name="description" content="<?php echo $month_desc; ?>">
<meta name="keywords" content="<?php echo $month_name . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $month_link; ?>">
<meta property="og:title" content="<?php echo $month_title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $month_desc; ?>">
<meta property="og:type" content="object">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $month_title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $month_desc; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
    if (is_author()) {
        $author_title = get_the_author();
        $author_name = mb_strtolower($author_title);
        $author_desc = 'Author: ' . $author_title . ' – ' . $options['metadescription'];
        $author_link = get_author_posts_url(get_the_author_meta('ID'), get_the_author_meta('user_nicename'));
?>
<meta name="description" content="<?php echo $author_desc; ?>">
<meta name="keywords" content="<?php echo $author_name . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $author_link; ?>">
<meta property="og:title" content="<?php echo $author_title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $author_desc; ?>">
<meta property="og:type" content="object">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $author_title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $author_desc; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
    if (is_singular('post')) {
        if (have_posts()) : while (have_posts()) : the_post(); endwhile; endif;
        $title = get_the_title();
        $excerpt = esc_attr(wp_trim_words(get_the_excerpt(), 25, '...'));
        $link = get_the_permalink();
        $cat_name = mb_strtolower($title); $cat_name = str_replace('"', '', $cat_name); $cat_name = str_replace('“', '', $cat_name); $cat_name = str_replace(',', '', $cat_name); $cat_name = str_replace('!', '', $cat_name); $cat_name = str_replace('”', '', $cat_name);
?>
<meta name="description" content="<?php echo $excerpt; ?>">
<meta name="keywords" content="<?php echo $cat_name . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $link; ?>">
<meta property="og:title" content="<?php echo $title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $excerpt; ?>">
<meta property="og:site_name" content="<?php bloginfo('name'); ?>">
<meta property="og:type" content="article">
<?php
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            echo '<meta property="article:tag" content="' . $tag->name . '">';
        }
    }
?>
<meta property="og:image" content="<?php the_post_thumbnail_url('large-thumb'); ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $excerpt; ?>">
<meta name="twitter:image" content="<?php the_post_thumbnail_url('large-thumb'); ?>">
<?php }
    if (is_page()) {
        if (have_posts()) : while (have_posts()) : the_post(); endwhile; endif;
        $title = get_the_title();
        $excerpt = esc_attr(wp_trim_words(get_the_excerpt(), 25, '...'));
        $link = get_the_permalink();
        $cat_name = mb_strtolower($title); $cat_name = str_replace('"', '', $cat_name); $cat_name = str_replace('“', '', $cat_name); $cat_name = str_replace(',', '', $cat_name); $cat_name = str_replace('!', '', $cat_name); $cat_name = str_replace('”', '', $cat_name);
?>
<meta name="description" content="<?php echo $excerpt; ?>">
<meta name="keywords" content="<?php echo $cat_name . ', ' . $options['metakeywords']; ?>">
<meta property="og:url" content="<?php echo $link; ?>">
<meta property="og:title" content="<?php echo $title; ?> – <?php bloginfo('name'); ?>">
<meta property="og:description" content="<?php echo $excerpt; ?>">
<meta property="og:type" content="article">
<meta property="og:image" content="<?php echo $options['socialsimg']; ?>">
<meta property="og:locale" content="vi_VN">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@InitHTML">
<meta name="twitter:title" content="<?php echo $title; ?> – <?php bloginfo('name'); ?>">
<meta name="twitter:description" content="<?php echo $excerpt; ?>">
<meta name="twitter:image" content="<?php echo $options['socialsimg']; ?>">
<?php }
}

Sử dụng

Ở header.php, gọi lại hàm này dưới wp_head();, ta được:

<?php
    wp_head();
    init_meta();
    $tem_url = get_bloginfo('template_url');
?>

Vậy là chúng ta đã hoàn thành. Kết quả:

Share bài viết lên Facebook

Kết luận

Chúng ta kết thúc series Dự án Blog cá nhân tại đây. Bây giờ, các bạn hãy học cách tạo một máy chủ, sau đó mình sẽ thực hiện tiếp một series hướng dẫn đưa website này lên mạng. Chúc các bạn thành công!

Tham khảo thêm:

5/5 (2 bình chọn)