Random header image at Wythagy

Dynamic Titles with PHP

Dynamic Titles with PHP

January 20th, 2009  |  Published in Tutorials, Web Design  |  3 Comments

We have received quite a few requests after our posting on Dynamic Web Site Basics in PHP on various other dynamic uses of PHP, one main request being the use of dynamic title generation.  Dynamic title generation uses PHP to produce unique titles for each page within your website, which is necessary to maintain optimum Search Engine Optimization (SEO).

We will make the assumption that you have taken the tutorial on Dynamic Web Site Basics in PHP.  If you have a strong grasp on that tutorial, creating dynamic titles will be a breeze for you.  We will be using our same template file, index.php, that we made in the previous tutorial with the same footer.php and header.php external files.  To create dynamic titles, you will need to have index.php and header.php ready.

Initializing the Title Variable

The first step is creating a variable within PHP that will allow us to pass along information to the header.php file that contains our <title> and </title> tags.  Because this variable is page specific, we will put that variable in our index.php file.  The code should look something like this (including our previous PHP code to include the header.php file):

<?php

$title = ‘Your Title’;
$head = $_SERVER['DOCUMENT_ROOT'] . ‘/path-to-file/header.php’;
require $head;

?>

You can now replace Your Title with an appropriate title for each page you make.  We will pass this variable along to our header.php file.  NOTE: Be sure to initialize your title variable before you call the header.php file.

Pass The Variable to Header.php

Now that we have defined our variable in index.php, we need to pass that variable to our <title></title> tags.  This requires a simple PHP script within those tags that looks like this:

<head>
<title><?php echo $title; ?></title>

The PHP code above simply tells the browser to put the contents of our variable $title in between our title tags.

Conclusion

With these two simple additions, we can now use index.php as a template, replacing the contents of $title with whatever we desire on each unique page.  With the addition of that simple PHP line in header.php, you don’t need to touch that file again in reference to making a title.

Tip

This same exact technique can be used with your Meta tags such as your description tag, which should also have unique content relative to it’s page to ensure proper Search Engine Optimization.  Simply initialize a variable, place it before your call to header.php and place a code similar to the one above in your meta tags:

<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $metaDescription; ?>” />

Responses

  1. mark says:

    February 5th, 2009at 9:25 am(#)

    Yes, it’s very useful, how can this be done in Wordpress?

    Regards,

  2. admin says:

    February 5th, 2009at 6:44 pm(#)

    Mark,

    WordPress actually does this automatically and is a great example of the power of PHP. WordPress dynamically creates your titles based on your post titles.

    However, if you are looking to have even further control over your titles, especially for SEO purposes, you should use a plugin such as the All In One SEO Pack by Michael Torbert. Not only is it free, it really expands upon the SEO capabilities of WordPress.

    Best of luck,

    The Wythagy Team

  3. jailyn says:

    May 25th, 2009at 11:02 am(#)

    i would love to create this website thanks for tips.

Leave a Response

Tag Cloud

News (2)
Tutorials (2)
Web Design (2)

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Get a Free Quote


Contact Us

info@wythagy.com
888.952.2221 Tel
214.988.5851 Fax
5760 Legacy Drive
Suite B3-511
Plano, TX 75024