Adding Custom Meta Descriptions to WordPress Posts Without Using a Plugin

It is true that meta descriptions no longer play a role as a ranking factor, but they still hold a lot of value. This is considering the fact that Google and other search engines use meta descriptions in their SERPs (search engine result pages) which makes meta descriptions a great way to attract user attention and make them click on your listing.

Adding Meta Descriptions to WordPress Posts

By default wordpress does not have a field that allows a user to add a meta description, but this can be easily achieved using custom fields and then calling this field from within the head section when a page loads. The following are the steps required to achieve this:

  1. Create a custom field using the wordpress dashboard
  2. Edit your theme’s functions.php page and add a function to call this custom field.

Let’s see how this can be done in details:

1.) Creating the Custom field

The first step is the create a custom field. This can be done as follows:

Step 1: Login to your wordpress dashboard.

Step 2: Create a new post or open an existing post to which you need to add a meta description.

Step 3: Make sure that custom fields are being displayed. If not, click Screen Options located in the top right corner of your dashboard and make sure the check-box next to custom fields is checked as shown in the image below.

Step 4: Scroll down to the bottom of the post editor and you should now be able to see a Add New Custom Field area.

Step 5: Click Enter new and in the Name section write down the text description and in the Value section write down the custom meta description of the respective article as shown in the image below.

Step 6: Click on ‘Add Custom field’.

Now the next step is to create a function that calls this custom field and outputs it in the head section of the site. Remember that the name of the custom field we need to call is description (all lower caps).

2.) Adding the Function to Functions.php

Open your theme’s functions.php page and add the following code to it:

/*Display custom meta description or the post excerpt */
function add_custom_meta_des(){

#Homepage Meta Description
if( is_home() || is_front_page() ){
	$meta_des = "Enter your homepage meta description here"; #Edit here
	echo '';

#Single Page Meta Description
if( is_single() ){
	$des = get_post_meta( get_the_id(), 'description', true);
	if( ! empty( $des )  ){
		$meta_des = esc_html($des);
		echo '';
add_action( 'wp_head', 'add_custom_meta_des', 4 );

This is how the above code works:

  • The first line of the code checks if the current page is the homepage and if yes, displays the meta description entered in the code.
  • If it is not the homepage the code checks if the page is a single post page and if yes, it checks if the page has a custom field named description and outputs the custom field if present. If not present, it outputs nothing.

So this is how easily you can displays custom meta tags in your posts. You can also use the same code to display meta keywords and other related meta tags. If you have any questions, feel free to leave it in the comments below.
  • Categories