Develop your own SEO features with WordPress: the description

After studying the title tag, we are going now to look at the meta description tag.
This field is usually a brief summary of the page. Regarding search engines, the description has also to contain the main keywords to better classify the post.

Structure Overview

The general structure of the previous post was:

1
2
3
4
5
add_action ( 'wp_head', 'my_seo_title');
 
function my_seo_title function () {
    // Function to print the title
}

We will now add a function to manage description. We take the opportunity to combine our SEO functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 add_action ( 'wp_head', 'my_seo_features', 1);
 
function my_seo_title function () {
    // Function to display the title
}
 
function my_seo_description function () {
    // Function to display the description
}
 
function my_seo_features function () {
    my_seo_title ();
    my_seo_description ();
}

About add_action, we add the parameter ‘1’. This parameter will put our function at the beginning of the list of actions to perform in wp_head(). In this way, our tags are located at the top of the header, rather than the bottom as in the previous post.

Generate description tag

Before starting, we have to define the content of this tag, for each case. For page ‘Category’, the answer is simple, since the categories have a description. But in the case of posts, archives, tags, what to do?

The following table summarizes what we can do:

Page Type Description
1. Home blog description
2. Post or page The excerpt of the post or the first few lines
3. Category The description of the category
4. Tags The summary of the first item of the list of posts
5. Research
6. Archives blog description

Why these choices?

  • Lines 1 and 3, we have a description, there is nothing to do
  • Line 2, we can estimate that the summary of the post gives a good overview of the post itself,
  • Lines 4 and 5, we can also consider that the posts corresponding to a tag or a search results, look more or less, similar. Therefore, the summary of the first article can be significant,
  • Line 6 (archives), it seems difficult (even dangerous) to build a description based on several posts that can be very different.

How to collect information about the articles? Using global variables post and posts, which are generated by WordPress before loading pages.

We obtain the following PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function my_seo_description() {
  global $post, $posts;
 
  if ( (is_single() || is_page()) ) {
      $page_description = get_description($post);
  }
  elseif (is_category()) {
      $page_description = category_description();
  }
  elseif ( is_tag() ) {
      $page_description = get_description(current($posts[0]));
  }
  elseif (is_date()) {
      $page_description = get_bloginfo('description');
  }
  elseif (is_search()) {
      if (sizeof($posts) > 0) {
          $page_description = get_description(current($posts[0]));
	}
      else {
          $page_description = get_bloginfo('description');
	}
  } else { /* is_home normalement */
      $page_description = get_bloginfo('description');
  }
  if ($page_description != '') {
      $page_description = htmlspecialchars(stripslashes(strip_tags(str_replace("n", '', $page_description))));
      echo '<meta name="description" value="'.$page_description.'">'."n";
  }
}

Some explanations:

  • Line 5: a single post is displayed, the global variable $post is available,
  • Lines 11 and 18, we have a list of posts to display. The global variable $posts is available. With the function current(), we get the first element (post),
  • In other cases, we retrieve the description of the blog,
  • At the end, we try to filter as much as possible, the content of the description to avoid any incident during the display.

The get_description() can be:

 function get_description($current_post) {
$max_length = 200;
$string = '';
// If post has an excerpt?
if ($current_post->post_excerpt != '') {
$string = wp_html_excerpt ( $current_post->post_excerpt, $max_length);
}
else {
// No excerpt, try to find the

Autres articles de cette série

  1. Code your own SEO function with WordPress: the keywords (19 février 2009)
  2. Développer vos propres fonctions SEO avec WordPress: les mots-clés (19 février 2009)
  3. Develop your own SEO features with WordPress: the description (12 février 2009)
  4. Développer ses propres fonctions SEO avec WordPress: la description (12 février 2009)
  5. Develop your own SEO functions with WordPress: the title (5 février 2009)
  6. Développez vos propres fonctions SEO avec WordPress: le titre (5 février 2009)
  7. 6 plugins SEO pour WordPress (5 janvier 2009)