WordPress Meta Keywords by RankYa

WordPress CMS for Blog Posting

Using WordPress popular SEO Plugins such as Yoast which seem to NOT use meta keywords tag anymore? Let’s fix that so that you too can continue using it (just because some SEO guru/s say that you shouldn’t doesn’t mean that you shouldn’t.).

Here you’ll fine some WordPress specific PHP codes you can insert in to your Theme template file so that your blog posts automatically insert meta keywords using Tags you tagged for the post you created.

Code that Generates Meta Keywords Tag

<?php
global $wp_query;
if ( is_single() ) : ?>
<?php //do this check first
$posttags = get_the_tags(); ?>
<?php if ($posttags) : ?>
<?php
$get_keys = array_keys($posttags);
$last_key = array_pop($get_keys);
?>
<meta name="keywords" content="<?php
foreach($posttags as $key => $value) {
$tag_name = $value->name; // tag name
if($key == $last_key) {
echo $tag_name;
} else {
echo $tag_name.', ';
}
}
?>">
<meta itemprop="keywords" content="<?php
foreach($posttags as $key => $value) {
$tag_name = $value->name; // tag name
if($key == $last_key) {
echo $tag_name;
} else {
echo $tag_name.', ';
}
}
?>">
<?php //smart to else it
else: ?>
<!--do nothing for if NO posttags-->
<?php endif; // end if posttag check ?>
<?php endif; // end if single check ?>

How to Use It? The Process

Simply place this code in header.php file of your WordPress theme, make sure it is placed within <head>anywhere within head portion</head>

Where is that file?

Login to Web Hosting Account > file manager > public_html > wp-content > themes > YourThemeName > header.php file

Warning: make sure you backup (download) your current header.php before you modify that file. If for some reason something goes wrong, you can simply re-upload your backedup header.php file to the same location on your server.

Meta Keywords for WooCommerce

<?php
global $wp_query;
if ( is_single() && !is_product() && !is_front_page() && !is_home() && !is_page() && !is_paged() && !is_archive() && !is_category() && !is_feed() && !is_attachment() && !is_404() ) : ?>
<?php //do this check first
$posttags = get_the_tags(); ?>
<?php if ($posttags) : ?>
<?php
$get_keys = array_keys($posttags);
$last_key = array_pop($get_keys);
?>
<meta name="keywords" content="<?php
foreach($posttags as $key => $value) {
$tag_name = $value->name; // tag name
if($key == $last_key) {
echo $tag_name;
} else {
echo $tag_name.', ';
}
}
?>">
<meta itemprop="keywords" content="<?php
foreach($posttags as $key => $value) {
$tag_name = $value->name; // tag name
if($key == $last_key) {
echo $tag_name;
} else {
echo $tag_name.', ';
}
}
?>">
<?php endif; // end if posttag check ?>
<?php elseif ( is_product() ): ?>
<?php //do this check first
$terms = get_the_terms( $post->ID, 'product_tag' );
?>
<?php if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) : ?>
<?php
$get_keys = array_keys($terms);
$last_key = array_pop($get_keys);
$rankyacheck = array();
?>
<meta name="keywords" content="<?php
foreach ( $terms as $term => $value) {
$rankyacheck = $value->slug;
if($term == $last_key) {
echo $rankyacheck;
} else {
echo $rankyacheck.', ';
}
}
?>">
<meta itemprop="keywords" content="<?php
foreach ( $terms as $term => $value) {
$rankyacheck = $value->slug;
if($term == $last_key) {
echo $rankyacheck;
} else {
echo $rankyacheck.', ';
}
}
?>">
<?php endif; // end if product check ?>
<?php //smart to else it
else: ?>
<!--do nothing for if NO posttags-->
<?php endif; // end if single check ?>

How to Use It?

Same Process as mentioned above.

What Does the WooCommerce Version Do?

It does the same thing for blog posts, it generates meta keywords automatically pulling in post tags, furthermore, it also pulls Product Tags for WooCommerce product and generates that automatically as well. How great is that? That’s the benefits of learning with RankYa.

Yoast Meta Keywords Missing?

Popular Search Engine Optimization plugins such as Yoast SEO Plugin have decided to disable meta keywords tag, but you can now use RankYa custom WordPress codes to generate meta keywords tag automatically.

Want to get a hold of more WordPress SEO codes like this? Simply check out public assets section of RankYa blog

Author: RankYa

RankYa: Online Entrepreneur, Qualified Web Developer, Google AdWords and Google Analytics Certified Professional. Specialist in: SEO, Website Optimization, WordPress, Structured Data, JSON-LD, Microdata, Microformats, RDF, Schema.org Vocabulary, HTML5, Advanced Image Optimization, Google Search Console, Google Webmaster Guidelines, Social Media Marketing, Facebook marketing and YouTube video ranking.

Thank you for sharing this blog post. Description: 'WordPress Meta Keywords by RankYa shares custom codes to generate meta keywords tag automatically.'