Develop your own SEO functions with WordPress: the title

Some previous tests of SEO plugins, made me quite disappointed. However, they allowed me to explore the different methods used. These methods are simple and can easily be developed into a theme, for example.
This article is part of a series of three posts: the first (this one) will address the title and the robots meta tag, the second will speak about the description field and the last will be focused on keywords field.


We just want some functions to fill automatically the following tags:

  • Title
  • meta name="robots" content="..."
  • meta name="description" content="..."
  • meta name="keywords" content="..."

Functions must fit with different types of pages in WordPress.
I say automatically, because for me, the true added value of SEO functions is here. Automatic generation allows to save significant time.

Structure Overview

In this post and the followings, we will consider adding these features to a theme (the principle is the same for a plugin).

If your theme is developped properly, its header contains a call to wp_head () (in the HTML <head> and </head>.

We will change the behavior of wp_head() by adding our own functions (refer Hook in the documentation of WordPress).

Our php functions will have to

  1. Watch in what situation we are (page articles, archive page, homepage …),
  2. Collect available information,
  3. Format these information.

Point 1 is relatively easy through features available is_single(), is_page(), is_category()

Point 2 is a bit more difficult: we must no, as far as possible, overload our blog, with too many requests. We will have to collect existing information.

Point 3 is not so easy: we must be sure that the content will not generate an error.

To start, we open or create the functions.php in the root of the theme you are using. Then enter the following code:

add_action ( 'wp_head', 'my_seo_title');
function my_seo_title() {
   // Function to print the title

The my_seo_title() will contain everything you need to generate the title and robots meta properly.

Build the title and meta robots

The title that we want to build, will have the following structure: < string > | < blog title >

The meta tag robots is used to tell to search engines whether to scan the page, and / or follow the links it contains.

Here is the complete function:

function my_seo_title() {
  $separator  = '|';
  $blog_name  = get_bloginfo('name');
  if((!is_paged() ) &amp;&amp; ( is_single() || is_page() || is_home() || is_category())) $follow_content = 'index, follow';
  else $follow_content = 'noindex, follow, noodp, noydir';
  if ( (is_single() || is_page()) ) {
      $page_title = the_title('','', FALSE);
  elseif (is_category()) {
      $page_title = 'Category: '.single_cat_title('', FALSE);
  elseif ( is_tag() ) {
      $page_title  = 'Tag: '.single_tag_title('', FALSE);
  elseif (is_date()) {
      $page_title = 'Post of ';
      if (is_day()) {
          $page_title .= get_the_time('j F Y', FALSE);
      elseif (is_month()) {
          $page_title .= get_the_time('F Y', FALSE);
      elseif (is_year()) {
          $page_title .= get_the_time('Y', FALSE);
  elseif (is_search()) {
       $page_title = 'Search results for: '.$_GET['s'];
  if ($page_title != '') $page_title .= ' '.$separator.' ';
 $page_title .= $blog_name;
  echo ''."n";
  echo ''."n";

The source code is available here:

Titre: my_seo_title (234 clics)
Nom du fichier: my_seo_title.txt
Taille: 1 KB

In line 5 and 6, we tell to search engines what they have to do with the current page: For a page, a post or a category, we ask them to scan the content and follow the links. In all other cases, we ask them to do nothing.

From line 8, we try to know where we are, and build the title.
In line 32, if $page_title is empty, we could not find on which page we are.
If this variable is not empty, we will add a separator.

In line 33 we complete the title by the name of the blog.

Lines 34 and 35 ensure the display of tags. In line 34, we try to clean the title of any dangerous characters. This cleaning may also be made with WordPress features like sanitize_title().


We have now a theme with title, and robots meta for each of its pages.

As you can see, these functions are not really complex. In a few lines of code, we already obtain good results. We will see in a next post the case of the meta tag description.

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)