How to Add Meta Description without Plugin

wordpress-coding

You don't need any plugin to add a WordPress meta description

Adding a meta description in WordPress is relatively straightforward. You don’t need any SEO plugins at all. Here is how.

Automattic, the owner of WordPress, has now divided the theme into two styles: the regular old classic theme and the newer block theme. If you are looking into your ‘/wp-content/themes/‘ folder and find ‘index.php,’ you have the classic theme. If you find ‘index.html‘ (usually in ‘/wp-content/themes/<theme_name>/templates‘), you are looking at the new block theme. Nevertheless, the standard ‘function.php‘ will be the main file for theme customization in both styles.

These are the steps to add a meta description to your page header (inside the HTML <head> part).

 

wp-add-nonce-1

Find your theme and locate the function.php file

The function.php is usually in the ‘/wp-content/themes/<theme_name>‘ folder

wp-add-nonce-2 wp-add-nonce-3

Add the code below to the function.php file

### function.php

function add_meta_description() {

    echo '<meta name="description" content="<?php echo get_the_excerpt(get_queried_object()); ?>">'; 

add_action( 'wp_head', 'add_meta_description' );

 

This will add your post (and page) excerpt to the meta description. Some people argue that the post/page excerpt is a different summary from the meta description. However, based on what Google describes as a good meta description, the post excerpt should be the same as the meta description. It should be 160 characters or less that summarize what the post/page is about.

Enable Excerpt to the static page

Before I forget, only some templates enable excerpts for the static page. If you have seen the meta description in your post but have yet to be added to your static pages (about, services, portfolio page, etc), add this code to the ‘function.php‘ file.

 

### function.php

/* Add excerpt for the static page for meta description */ add_post_type_support( 'page', 'excerpt' );

 

Posted in WordPress

Leave Your Comment

(*) These fields are required