给WordPress添加Keywords和Description标签,增强SEO功能

WordPress是世界上最受欢迎的内容管理系统,占据了三分之一的份额。然而,WordPress本身缺少一些SEO功能,如Keywords和Description标签。尽管有几个功能非常完善的SEO插件,如Yoast SEO,但这类插件臃肿而繁芜,且要付费才能使用高级功能。

今天完善了一个外贸型WordPress网站,顺便将基本的SEO功能加上了。其实非常简单,只涉及两个主题文件,functions.phpheader.php。按照WordPress开发的最佳原则,此类改动应该在子主题中进行。我修改的是基于twentyseventeen的子主题。

首先,在functions.php中加上两个可自定义的主题选项。以下代码片段让用户可以自定义首页(启用frontpage.php)的关键词和站点描述。

<?php
    // Add more customization options
    function my_customize_register( $wp_customize ) {
        // SEO keywords
        $wp_customize->add_setting(
            'seo_keywords', array(
                'default'           => 'WordPress',
                'transport'         => 'postMessage',
                'sanitize_callback' => 'my_sanitize_seo_keywords',
            )
        );
        
        $wp_customize->add_control(
            'seo_keywords', array(
                'type'     => 'text',
                'label'    => __( 'SEO keywords', 'twentyseventeen' ),
                'description' => __('SEO keywords mainly for the frontpage, separated by comma', 'twentyseventeen'),
                'section'  => 'title_tagline',
                'priority' => 57,
            )
        );

        // SEO description
        $wp_customize->add_setting(
            'seo_description', array(
                'default'           => 'A beautiful WordPress website',
                'transport'         => 'postMessage',
                'sanitize_callback' => 'my_sanitize_seo_description',
            )
        );
        
        $wp_customize->add_control(
            'seo_description', array(
                'type'     => 'text',
                'label'    => __( 'SEO description', 'twentyseventeen' ),
                'description' => __('SEO description mainly for the frontpage', 'twentyseventeen'),
                'section'  => 'title_tagline',
                'priority' => 58,
            )
        );
    }
    add_action( 'customize_register', 'my_customize_register' );

    // Sanitize SEO keywords input
    function my_sanitize_seo_keywords( $input ) {
        return esc_attr($input);
    }

    // Sanitize SEO description input
    function my_sanitize_seo_description( $input ) {
        return esc_attr($input);
    }
    // End more customization options
?>

接着,在header.php中原有的2个meta标签下方,增加以下代码片段,以便在浏览器中显示关键词和站点描述。

<!-- SEO begins -->
    if (is_front_page()) {
        $keywords = get_theme_mod("seo_keywords", "WordPress");
        $description = get_theme_mod("seo_description", "A beautiful website powered by WordPress");
    } else {
        if ($post->post_excerpt) {
            $description = $post->post_excerpt;
        } else {
            $description = "This is the inner page";;
        }

        if (function_exists('is_product') and is_product()) { // Addd product tags if this page is a woocommerce product page
            $product_tags = get_the_terms($product->ID, 'product_tag');
            foreach ($product_tags as $product_tag) {
                $keywords = $keywords . $product_tag->name . ', ';
            }
        } else { // Not a product page
            $tags = wp_get_post_tags($post->ID);
            foreach ($tags as $tag) {
                $keywords = $keywords . $tag->name . ', ';
            }
        }
        $keywords = $keywords . get_bloginfo('name');
    }
?>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $description; ?>">
<!-- SEO ends -->

以上代码考虑到了WooCommerce产品页面的SEO,虽然较简单。

该twentyseventeen子主题已经上传到Github