How to Add Meta Descriptions to WordPress Themes Using functions.php

While working on an SEO for WordPress course recently, I was looking for a way to manually add meta descriptions to a theme from the functions.php file.

After playing around a bit, I came up with this solution:


function custom_get_excerpt($post_id) {
$temp = $post;
$post = get_post( $post_id );
setup_postdata( $post );
$excerpt = esc_attr(strip_tags(get_the_excerpt()));
wp_reset_postdata();
$post = $temp;
return $excerpt;
}
function custom_add_meta_description_tag() {
?>
<meta name="description" content="<?php if ( is_single() || is_page() ) {
$excerpt = custom_get_excerpt(get_the_ID());
echo $excerpt;
} else {
bloginfo('description');
}
?>" />
<?php
}
add_action('wp_head', 'custom_add_meta_description_tag', 1);

This of course raises the bigger question of why would you want to do this instead of using a plugins like SEO by Yoast or All in One SEO.  I don’t think that you actually would :p I just wanted to demonstrate how to do it.

If you have a more elegant solution, please suggest away!

8 thoughts on “How to Add Meta Descriptions to WordPress Themes Using functions.php

  1. Hi Zac,

    Thanks for sharing it! I like it 🙂 SEO by Yoast or All in One SEO or the others are a little bit of bloatware I think and the fewer plugins we have the better too. In March the news came out that Yoast WP SEO was open to SQL injection, for example. So that is why 🙂

    Like

  2. I’m working on a project now where a page is created by a theme but there’s no access to it outside rudimentary basics. This is a great option since even Yoast can’t see it.

    Like

  3. Hey, I’ve been searching many hours for such solution. It saved my day because nothing else works with my wordpress theme and I didn’t want to use plugins to solve this problem.
    THANKS!

    Like

  4. Hello Zac Gordon what I experienced is Instead of using SEO by yoast plugin. I want to use custom meta tags. I know which one are the best meta tags that google loves. Yoast’s plugin doing less job in this case. They are asking to pay more than one keyword. Even If I will upgrade to premium they will still miss a lot of things that effect our ranking. So What I am doing is I am making custom meta box for post and pages. I am going to use those meta box in wp_head.

    Practical Example:
    If you are going to search web designer in India then check first result open the website then press ctrl+u You will see they are not using any plugin and still on top number with just correct tags.

    Like

  5. Thanks, Zac you’re the best. You just saved me days of endless searching. I couldn’t use Yoast or WPSEO as they conflicted with other plugins on my site. I found a partially working code from elegantthemes blog to add meta tags to head but the code to display excerpt wasn’t working and the author won’t update the code. Every time I search online, the only thing people recommend is to use Yoast. You’re the only one who provides really working implementation and I’ve added a link to your site in the code so I can come back here again in the future. Thanks again for the help.

    Like

Leave a reply to Szabesz Cancel reply