WordPress Custom Meta Description

DSDalton SuttonMarch 19, 2024
View AllHide Notes

How To Install

  1. Open your WordPress theme's functions.php file or a plugin's PHP file.
  2. Copy and paste the provided PHP snippet into the PHP file.
  3. Save the file.
  4. Adjust as needed.
<?php 

// add a meta description to the head
function daltonsutton_meta_description() {
    if (is_home()):
        echo '<meta name="description" content="'.get_bloginfo('description').'" />'.PHP_EOL;
    endif;
    if (is_single() || is_page()):
        if ($excerpt = get_the_excerpt()):
            echo '<meta name="description" content="' . $excerpt . '" />'.PHP_EOL;
        endif;
    endif;
}
add_action('wp_head', 'daltonsutton_meta_description');