Insert Meta Description HTML Tag for each post and page (WordPress)

To totally unlock this section you need to Log-in

The meta description is a snippet of up to about 155/160 characters (it depends on which search engines they are displayed) – a meta tag in HTML – which summarizes a page’s content. Search engines show it in search results mostly when the searched-for phrase is within the description.

The meta description is an HTML tag, which looks like this in the HTML code for the page:

<meta name="description" content="A page's description, usually one or two sentences."/>

Its purpose is simple: it needs to get someone searching on Google and other search engines to click your link. In other words, meta descriptions are there to generate click-throughs from search engines.

Search engines say there is no direct ranking benefit from the meta description – they don’t use it in their ranking algorithm. But there is an indirect benefit: Google uses click-through-rate (CTR) as a way of working out whether you’re a good result. If more people click on your result, Google considers you to be a good result and will – based on your position – move you up the rankings.

Now, in WordPress there is still no way to add, by default, the excerpt of a post or page, as the value of the property description of the meta tag, and that's why the following code can be useful, as it does exactly this:

function custom_get_excerpt($post_id) {
$temp = $post;
$post = get_post( $post_id );
setup_postdata( $post );

$excerpt = esc_attr(strip_tags(get_the_excerpt()));

wp_reset_postdata();
$post = $temp;

return $excerpt;
}

function add_meta_description_tag() {
?>
<meta name="description" content="" />
<?php
}
add_action('wp_head', 'add_meta_description_tag', 1);
?>

This code can be used directly in the functions.php file in your WordPress installation, or by including it into a new simple plugin (by creating a custom .php plugin file), to be sure it to keep it valid and available after a major future WordPress upgrade.

As additional info, page type in WordPress does not have any Excerpt section in Edit mode (in contrast to post type), but we can easily enable it, even in the same plugin in which wil put the above code, by using the following code:

add_post_type_support( 'page', 'excerpt' );

This code modifies the default WordPress content type ‘page’ to add support for excerpts.

Category

Summary
Article Name
Insert Meta Description HTML Tag for each post and page (Wordpress)
Description
How to insert meta description HTML tag without any plugin? Here you can find out how to do that in Wordpress.
Author
Publisher Name
Heelpbook.net

1 thought on “Insert Meta Description HTML Tag for each post and page (WordPress)”

Comments are closed.