php Code for Meta Description without Plugins

Paste this code on functions.php and automatically set meta description on Post.

function add_meta_description_thekroyaard() {
if (is_single()) {
$post = get_post();
$excerpt = strip_tags($post->post_excerpt);
if ($excerpt == '') {
$excerpt = strip_tags($post->post_content);
$excerpt = substr($excerpt, 0, 160);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt .= "...";
}
echo '<meta name="description" content="' . $excerpt . '">';
}
}
add_action('wp_head', 'add_meta_description_thekroyaard');