How to Generate Post Meta Keywords and Meta Description Without Plugin in WordPress

Published on Thursday, February 25, 2016

Meta keywords and Meta description are 2 important elements in SEO. By default, WordPress does not provide Meta keywords and description for single post. Some users just manually add meta information or use plugins.

If you are a blogger who does not like to use too many plugins and you want to do more modifications by yourself. you can try to copy the following code directly then put it into the section of <head> of Header.php file, if you can find other meta lines, just put the code right after those meta information lines:

<!-- my code start -->
<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<meta name="description" content="<?php the_excerpt_rss(); ?>" />
<meta name="keywords" content="<?php 
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->name . ', ';
 ?>" />
<?php endwhile; endif; elseif(is_home()) : ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php endif; ?>
<!-- my code end -->