Learn How to Add Meta Descriptions in WordPress Without Plugins. Two Simple Methods: Custom Fields or Automatic Generation with a Code Snippet

Adding a meta description to your WordPress site is crucial for improving SEO and attracting clicks from search engine results pages.

By default, most WordPress themes don’t generate meta descriptions automatically after a clean installation. While plugins can help, sometimes you want a lightweight, code-based solution for this simple task.

In this post, we’ll show you how to manually add a meta description to all page types using custom code—no plugins required. Let’s dive in!

So, what makes these methods great? The key is that we can combine both approaches.

First, enter the code that automatically generates a meta description with a maximum of 160 characters. Then, enter another code that creates a custom field in the post editor to manually set the meta description.

When you add a meta description in the custom field, the automatically generated text is overridden, and only the text you added will be output. This means you’ll still have a meta description, even if you simply write and publish the post without any further action. Here, you have both solutions and choices.

Add Meta Description to Single Post or Page

There are two methods below and each one has a code, we suggest you use both.

First Method: Automatic Generation of Meta Description

This code should be placed in the

<head>
section (usually in
header.php
).


<?php
if (is_singular(array('post', 'page')) && !is_front_page() && !is_home()) {
global $post;
$custom_meta_desc = get_post_meta($post->ID, '_meta_description', true);
$meta_desc = !empty($custom_meta_desc) ? $custom_meta_desc : apply_filters('the_content', $post->post_content);
$meta_desc = strip_tags($meta_desc);
$meta_desc = strip_shortcodes($meta_desc);
$meta_desc = str_replace(array("\n", "\r", "\t"), ' ', $meta_desc);
$meta_desc = substr($meta_desc, 0, 160);
<meta name="description" content="<?php echo esc_attr($meta_desc); ?>" />
}
?>

Second Method: Manual Meta Description Using Custom Field

This method requires more code, but it’s recommended for greater control. Add tis  in

functions.php

Code to Add a Meta Description Field for ‘post’ and ‘page’

  • Retrieve the existing meta description, if any
  • Update character count in real-time
  • Trim input if it exceeds the max length


function add_meta_description_field() {
add_meta_box(
'meta_description_box',
'Meta Description', // Box title
function($post) {
$meta_description = get_post_meta($post->ID, '_meta_description', true);
$max_length = 160; // Define maximum character length
?>
<label for="meta_description">Enter a meta description (max <?php echo $max_length; ?> characters):</label>
<textarea id="meta_description" name="meta_description" rows="3" style="width: 100%;" maxlength="<?php echo $max_length; ?>"><?php echo esc_textarea($meta_description); ?></textarea>
<p><span id="char_count"><?php echo strlen($meta_description); ?></span> / <?php echo $max_length; ?> characters used.</p>
<script type="text/javascript">
document.getElementById('meta_description').addEventListener('input', function() {
var maxLength = <?php echo $max_length; ?>;
var currentLength = this.value.length;
document.getElementById('char_count').textContent = currentLength;
if (currentLength > maxLength) {
this.value = this.value.substring(0, maxLength);
}
});
</script>
<?php
},
array('post', 'page'),
'normal',
'default'
);
}
add_action('add_meta_boxes', 'add_meta_description_field');

 

Add Meta Description to FrontPage

Adding a custom meta description to your WordPress FrontPage is crucial for improving your site’s SEO and making it more appealing in search engine results.

If you’ve created a static page to serve as your FrontPage, you can add a meta description using the Custom Field method introduced earlier. Alternatively, you can use the following code snippet for a quick solution.

Code to Add Meta Description for FrontPage:


<?php
if (is_front_page()) {
?>
<meta name="description" content="Add site meta description here...">
<?php
}
?>

### Example
Replace `Add site meta description here…` with a descriptive and engaging summary of your site, such as:

“Discover the best tutorials, tips, and resources for improving your digital skills. Your go-to platform for expert advice and insights!”

 

Add Meta Description to Blog Page or Any Page Displaying Posts

It’s important to add a meta description to your Blog Page (or any page that lists your posts) for better SEO and user engagement.

As mentioned earlier, you can use the **Custom Field method** to add a description manually. Alternatively, you can insert the following code snippet for an automated solution.

### Code to Add Meta Description for Blog Page


<?php
if (is_home()) {
?>
<meta name="description" content="Blog meta description here...">
<?php
}
?>

### Example
Replace `Blog meta description here…` with a brief summary of what your blog is about. For example:

“Stay updated with the latest tutorials, tech news, and insights. Explore articles tailored to your interests in technology and digital trends!”

This method ensures your Blog Page has a relevant and optimized meta description, improving its visibility and click-through rate in search engines.

Add Meta Description to Archive Pages (Category, Tag, etc.)

The following code snippet ensures a **meta description** is displayed for all archive pages, such as Category or Tag archives, but excludes Day, Month, and Year archives by default.

# Code Explanation
The condition:

if ( is_archive() && !is_year() && !is_month() && !is_day() )

applies the meta description to all archive pages except **Day**, **Month**, or **Year** archives.

To include a specific archive type (e.g., Yearly Archive), modify the condition by replacing:

!is_year()
with
is_year()
.

# WordPress Category or Tag Description
WordPress provides a built-in field to add a description for **Category** or **Tag** archives. The following code will take that description and use it as the **meta description** in the `head` section of your page.

### Code Example


<?php
if (is_archive() && !is_year() && !is_month() && !is_day()) {
?>
<meta name="description" content="<?php
$archive_description = get_the_archive_description();
// Remove paragraph tags from the description
$archive_description = preg_replace('/<p[^>]*>([\s\S]*?)<\/p>/', '$1', $archive_description);
echo esc_attr($archive_description);
?>">
<?php
}
?>

Notes:
– Default Descriptions:  Ensure you add descriptions to your Category and Tag fields in WordPress for this to work effectively.
– Custom Descriptions: If you wish to define custom descriptions for specific archive types, you can enhance this code to include additional logic.

This ensures that your archive pages are optimized for search engines with relevant and clear meta descriptions.