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 '<meta name="description" content="' . $meta_des . '" />';
}

#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 '<meta name="description" content="' . $meta_des . '" />';
	}
}}
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.

References:
WordPress Function Reference - get_post_meta()
WordPress Tweaks for Post Management

 
 
 
 

Comments

  1. Hondon says:

    This works but there are a few typos in it (mainly the variable $des in the second if block of the function. :-)

  2. Matteo says:

    I have tried this in some post and it work well. I seen that unfortunately it doesn't work for pages. Do you know how can I do?

    • M Mukesh says:

      Hi Matteo, Just change " if( is_single() " in the code to " if( is_single() || ( is_page() ) ".

  3. Bali says:

    thank you for the post, I try to implement to my new project, is working like charm but I have some little problem, the wp_head is showing also in header for meta name description. So in my header, I have 2 meta description, one from the custom meta description and other one is from wp_head. Do you know how to disable the wp_head showing meta description in header?

  4. Stafa says:

    My site have 2 meta description, how to delete 1 meta description?

  5. RobB says:

    Thanks for this, solved the issue perfectly for me :-)

  6. Roman says:

    Thank you very much. I tried other ways via php, but unsuccessfully. Your code is really beautiful and competent.

  7. voyko says:

    Very good article. I was wondering are the Custom Meta Descriptions same as Schema Markups. Sorry for the ignorant questions but I can't understand.
    Thx

    • M Mukesh says:

      Hi Voyko, no, schema is different. Schema basically helps you categorize your website into different sections using HTML tags. Meta description on the other hand helps you add a unique description to each post/page of your website.

  8. Voyko says:

    Hi Mukesh, I've tried this on my web page and it doesn't work.
    Do I have to make a child theme?
    Do I have to enter any details into the code?
    I am not a programer so I hope this aditional questions are not to much of a burden for you. Thx

    • M Mukesh says:

      No, child theme is not required. And you don't need to add any details into the code except the place where it says 'Edit here'. You can add your homepage meta description there. The single pages, you don't need to make any additional changes.

  9. Nate says:

    Hi, thanks so much for this!
    Why not allow the user to customize the description on the homepage too, though? It is hard-coded above for no real reason I could see.

  10. sanoj says:

    Hi There,

    Great Job!

    no need of searching more, After this.
    Good information really works!

    I need to customize the keywords also please suggest

  11. Raphael says:

    Thank you for this, is it ok I just ignore the homepage part since I won't be needing it and can this function overwrite any other meta description like from SEO plugins

  12. Yvonne says:

    Thank you for this! I have been searching for two days for a solution to use a custom field to add the meta description and I really didn't want to add a full plugin for one measly field. I was cobbling together various solutions but struggling. Then today, Google randomly served this up to me when I wasn't even looking for it. Go figure. This worked perfectly for me.

  13. romaink says:

    Hi Mukesh, how can i make this to work for posts as well as pages ? should a copy it twice with both " if( is_single() " and " if( is_single() || ( is_page() ) " ? thanks in advance, Romain

  14. JIllian says:

    can the category pages be added to this to use the category description as well?

  15. Pietro says:

    Good day folks!

    Thank you for sharing this valuable information with us. We tried the first part of the code on a website with just the homepage and worked with no issues.

    We intend to use the whole code on other websites where there are more pages besides the homepage this time, while using the kind suggestion from Mukesh to Matteo:

    " if( is_single() " in the code to " if( is_single() || ( is_page() ) ".

    Now, I am not too saavy on PHP; but a priori, I would say that I have to give the same name to all the custom fields as "description" in all the pages for the function to call the variable properly.

    Otherwise I would need to repeat the first part of the code for as many pages the websites do have. Is this correct?

    Thank you again for your help and interest.

    -Pietro

    • M Mukesh says:

      Hi Pietro,

      You need to put this code in your theme's functions.php page once. After that, you need to create one custom field named 'description' in your wordpress dashboard. Once you do that, you can then select 'description' from the drop down menu that will automatically appear for all articles.

      Check out point number 1: Creating the custom field in this article.

  16. rehman says:

    its the solution i am looking for i wanted to know how to add the meta descriptions for custom post types as well along with posts and pages so " if( is_single() || ( is_page() ) || (is_singular)". is the correct syntax to do? kindly guide please. thank you!

  17. Arkadiusz says:

    Hi,
    I am trying various ways to add a "meta description" to my product category pages.
    I added in the table "wp_termmeta": "description" to the "meta_key" column and a value to "meta_value", for individual "term_id" categories.

    adds to your example, e.g .:

    if (is_category ()) {
    $ des = get_term_meta (get_the_id (), 'description', true);
    if (! empty ($ des)) {
    $ meta_des = esc_html ($ des);
    echo '';
    }
    }

    I will be grateful for the tips

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.