Last week, we have posted about how to add meta description tag to your WordPress website without using any plugins and in this article, we will be showing you a way to add meta keywords tag to head section of website. First of all before proceeding any further, I would again recommend you to use a dedicated SEO plugin for optimizing your website for search engines. Without any doubts, a dedicated SEO plugin is always a better choice because they utilize all latest SEO techniques to help you improve your website’s search engine ranking. But if you are still looking for an easy way to add meta keywords tag to website head without using any plugins then carry on with the article.
We will be using post categories and tags to create meta keywords tag and use WordPress action wp_head to automatically add it to website head. Following is a nice little WordPress function that will add post categories and tags to meta keywords on WordPress posts.
Simple paste this WordPress snippet to your theme’s functions.php file. You don’t need to change anything in this code, as it will work out of the box for any website.
// add post category slug to body class in wordpress
function wcs_add_meta_keywords() {
global $post;
if ( is_single() ) {
$cats = get_the_category( $post->ID );
$tags = get_the_tags( $post->ID );
$keywords="";
foreach ( $cats as $cat ) {
$keywords .= $cat->cat_name . ", ";
}
foreach ( $tags as $tag ) {
$keywords .= $tag->name . ", ";
}
echo '<meta name="keywords" content="' . $keywords . '" />' . "\n";
}
}
add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );