View Full Version : PHP Include w/ Custom Meta Tags?

09-06-2007, 01:05 AM
Hi. I have a template that I use that calls a common header to all pages through a PHP include.

The problem is that I'd like to add custom meta tags individually to each page... but the template is calling the common header...

Each page will need unique meta tags so I need some sort of hack that lets me input the meta tags directly into the HTML while still pulling the common header.

Is this possible? Anyone have any tips?

thanks in advance.

09-06-2007, 01:08 AM
pull them out of a DB depending on which page it is?

09-06-2007, 01:15 AM
If it's only a few pages use something like this:

case 'test.php':
echo '<meta...>';

case 'contact.php':
echo '<other meta..>';

echo '<default meta...>';

09-06-2007, 01:37 AM
thanks for the responses... you know i figured it out though. It's not the cleanest way to do it i'm sure but it works...

If you're interested here's what I did.

In the header include file I put:

<meta name="description" content="<? if (isset($metadescription)) { echo $metadescription; } ?>">

Then on the individual page I put this immediately before I called the include:

$subtitle = "Page Title Here";
$metadescription = "Description Here";
$metakeywords = "Keywords Here";
require '../inc/header.inc.php'; ?>

When I load the page and view source it all comes out as clean HTML with the meta tags properly coded. So the search engines should be fine w/ it.

The subtitle is for the page title so each page can have a unique page title. The code in the include file is the same just w/ subtitle:

<title><? if (isset($subtitle)) { echo $subtitle; } ?></title>

As i said not the cleanest or easiest way i'm sure but it works. Hope someone else can use this.

By the way anyone have any thoughts on how to better this?

09-06-2007, 01:39 AM
INIGoesDR (lol)
his idea is probably the better solution.
I'd probably go with that.

09-06-2007, 05:20 PM
My solution is a little more complex, but in the end a lot cleaner and easier.

I have a static class called UI that I create per site (it has some resuable components, so it can inherit a base class if need be). This class has several methods to add things like scripts/css/etc. But these can be called from specific scripts to add to the common header. It could be changed to set/modify specific meta tags.

For example;




// ... some basic html here ...

foreach (UI::getStylesheets() as $css) {
echo '<link href="'.$css.'.css" rel="stylesheet" type="text/css" />';

// ... more basic html ...

This only works if your pages are executed before the header code. If you properly seperate model/views then you can add the stylesheet in the model, but that would be defeating the purpose of seperating business and presentation logic...

09-09-2007, 02:19 PM
If you're trying to create a small, easy to template based site, look into buffers and modrewrite...it's amazing what 50 lines of code can do.