meta description wordpress without plugin

Add the following code to the functions.php file.

function aditya_meta_description() {
    global $post;
    if ( is_singular() ) {
        $des_post = strip_tags( $post->post_content );
        $des_post = strip_shortcodes( $des_post );
        $des_post = str_replace( array("\n", "\r", "\t"), ' ', $des_post );
        $des_post = trim(mb_substr( $des_post, 0, 300, 'utf8' ));
      echo '<meta name="description" content="' . ($des_post) . '">'. "\n";
		 echo '<meta name="keywords" content="' . ($des_post) . '">'. "\n";
    }
    if ( is_home() ) {
      echo '<meta name="description" content="' . get_bloginfo( "description" ) . '" />' . "\n";
		   echo '<meta name="keywords" content="' . get_bloginfo( "description" ) . '" />' . "\n";
    }
    if ( is_category() ) {
        $des_cat = strip_tags(category_description());
      echo '<meta name="description" content="' . $des_cat . '" />' . "\n";
		  echo '<meta name="keywords" content="' . $des_cat . '" />' . "\n";
    }
}
add_action( 'wp_head', 'aditya_meta_description');



Leave a Reply