Automatically Add Meta Description to Header in WordPress Posts and Pages

/* Automatically Add MetaDescription to WordPress Header */
function wpflt_auto_add_meta_desc() {
    if ( ! is_singular() )
        return;
    $meta = strip_tags( get_post()->post_content );
    $meta = str_replace( array( "\\n", "\\r", "\\t" ), ' ', $meta);
    $meta = substr( $meta, 0, 155 );
    echo "<meta name='description' content='$meta'>";
}
add_action( 'wp_head', 'wpflt_auto_add_meta_desc' );

This is how you solve the “Document does not have a meta description” message on Google Chrome’s Lighthouse SEO Audit. To programmatically add meta descriptions to every post and page, use the above code in your child theme’s functions.php.

Note that we have used the is_singular() function which applies to all pages and posts. If you want to add auto meta description only for posts, use is_single() instead.

Similarly, if you don’t use WordPress to run blogs, and use only its pages, use the is_page() function to automatically add meta descriptions to all pages on your site.

The code will fetch the first 155 characters from the post content. However, you can change the character length to custom values (in line 7 of the above code). Increasing the length won’t make any difference as search engines only display content in limited length according to their standards (which change every now and then).