So what I'd like to do is generate a meta description like this one

<meta name="description" content="[post_title]Craftsman 3 in 1 tool set. [post_content]This is a sentence or a few words from the main content body text which was entered in the post or page description of item..."/>

The meta description would take the page title and a few words or a sentence from the main post content.

Currently no meta description is being generated as I can see. Could you recommend a way to work this in. I'd like to avoid using bloated plugins etc and it is not feasible to go through each post 1 by 1 and do this manually as there are thousands of posts and pages.

  • While this may not help solve the problem directly, it might provide you with insight on where to go next. Meta Tags in WordPress. YoastSEO is also one of the more widely used SEO plugins that you could look into. I believe it has an auto-populate meta description field. Nov 10, 2016 at 4:29
  • Thank you for your comment. I am planning to activate yoast but currently yoast is using OG description for social media etc and not the normal meta description tag (i'm after both) Appreciate the suggestion Greg
    – d.ariel
    Nov 10, 2016 at 10:31
  • You can setup a Meta Description template in the settings > titles & metas, but it does have the option under the post "Keywords" settings to adjust the meta description. Nov 10, 2016 at 14:07

1 Answer 1


You can use wp_head action to add something to head section. You can change the output according your needs.


function add_meta_des() {

        global $wp_query;
        $post = $wp_query->post;
        $page_id = $post->ID;
        $page_object = get_page( $page_id );
        $content = wp_trim_words($page_object->post_content,30);        
        $output="<meta name='description' content='".get_the_title()."--".$content."'/>";
        echo $output;
  • 2
    I think, instead of using is_single(), using is_singular() is more broad in this particular situation. Nov 10, 2016 at 5:10
  • Ranuka I put this at the bottom of my functions.php file and it did display but there were a couple of things wrong. #1 It showed up for some reason at the bottom of the page instead of at the top where my other meta data is located and #2 It did not get an excerpt. I'm not 100% sure how the .get_the_exceprt() is supposed to work or if that was just a sample but the get the title did work as expected. When using .get_the_excerpt() does that automatically get the first few words of the main body content?
    – d.ariel
    Nov 10, 2016 at 8:26
  • More info about WordPress excerpt : codex.wordpress.org/Excerpt and if you want to get it from content read wordpress.stackexchange.com/questions/141466/… . And add_action has two another optional Parameters. Check it here: developer.wordpress.org/reference/functions/add_action You can use those parameters display in right place.
    – Ranuka
    Nov 10, 2016 at 8:46
  • @Ranuka I read over the add action page you sent me but was unable to identify from that page how I can change where on the page the meta description is inserted. Possibly priority? But will that dictate the priority for the entire page or all the functions? If I set priority to 1 would that mean it loads at the top before css etc near the top of the header? Also I read the other links regarding the excerpt, using your code should have worked and pulled the "automatic" excerpt from the page but for some reason it didn't and it only used the title? Additional info very appreciated
    – d.ariel
    Nov 10, 2016 at 9:29
  • @d.ariel Ya, there was a problem. I updated the code. I tested it and it worked fine in my site. Now please try the updated code and let me know the result.
    – Ranuka
    Nov 10, 2016 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.