How best to display meta-tags on an included header file and on individual pages

Just a quick question. My website uses a header file which is included on all my pages. In that header file I list my meta-data, including a meta description. If I wanted to have a separate meta description for certain pages (I've read this is generally good practice), but continue to want to include the header in that page,  how would I best do that?

If I just entered another meta description, would that overwrite the one picked up in the  included header file, or would that confuse the crawler bots?

Many thanks for reading,

Adam
AdamTrying to learn phpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You certainly wouldn't want to have 2 matching meta tags in the header.

There are several ways you could achieve this, but you'd need some way of making your header include more dynamic.

You could wrap some DB logic in the header to pull the relevant description from a DB table:

<?php $desc = $db->query("SELECT desc FROM table WHERE pageId = xx")->fetchColumn(); ?>
<meta name="description" content="<?= $desc ?>">

You could pass an argument to the include using a query string:

<?php include header.php?desc=this+is+the+description ?>
...
<meta name="description" content="<?= $_GET['desc'] ?>">

If you're using a templating engine, then you could set a variable in the controller that the header could read in.

$data['desc'] = "This is the description";
$this->load->view("header", $data);
...
<meta name="description" content="<?= $desc ?>">

How you go about it will depend largely on what you currently have set up, so you may want to give us some more info about that.
AdamTrying to learn phpAuthor Commented:
Hi Chris,

Many thanks for getting back to me.

The current set up is pretty simple. I have one header file which all my pages link to. That header file has basic meta data including a description and keywords. I call my header file immediately after calling my initialise file so it's the second thing which happens on my code. i.e.
<?php require_once('private/initialize.php'); ?>
<?php include(SHARED_PATH .'/header.php'); ?>

Open in new window


Having had a bit more of a think about this, what are your thoughts on the following idea -

1. Any page which doesn't use the default meta tags (i.e. the ones in the header) I just put in that specific page before I call the header file. Something like this - using a string
<?php require_once('private/initialize.php'); ?>

<?php 
$meta_description = 'The meta description unique to the page goes here'; //this would be the description unique to this page
?>

<meta name="description" content=" '$meta_description' "/>

<?php include(SHARED_PATH .'/header.php'); ?>
<?php $page_title = 'About OJ'; ?>
<?php include(SHARED_PATH .'/navbar.php'); ?>

Open in new window


2. In the header file I add the following at the top of the header file:

<?php 
if (!isset($meta_description)) {$meta_description = 'this would be the default description';} //If the page title is not set, set it as 'Orango Japan'
?>

Open in new window


and further down...

<meta name="description" content=" '$meta_description' "/>

Open in new window


....
AdamTrying to learn phpAuthor Commented:
Okay, that didn't flag up any errors but I'm also not too sure if it's 'working'. It's not the most elegant of solutions but if it works I'm happy. However,  I'm slightly worried the Webcrawlers might not read this as they normally would. I'll do a quick search to see if there is a way to 'read' page meta descriptions and post back.

Thanks.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Chris StanyonWebDevCommented:
The idea you have will work. The Crawlers won't have an issue as they never see PHP code - only the generated HTML code. You could simplify it:

In you header file, have the following:

<?php $meta_description = isset($meta_description) ? $meta_description : "This is the default meta description"; ?>
<meta name="description" content="<?= $meta_description ?>"/>

Open in new window

And now in your page files you can choose to set a page specific meta or just leave it out to use the default:

<?php $meta_description = 'The meta description unique to the page goes here' ?>

<?php require_once('private/initialize.php'); ?>
<?php include(SHARED_PATH .'/header.php'); ?>

Open in new window

or

<?php require_once('private/initialize.php'); ?>
<?php include(SHARED_PATH .'/header.php'); ?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AdamTrying to learn phpAuthor Commented:
Hi Chris,

Many thanks again.   I was just in the process of testing it out there with the following page:  
http://www.exadium.com/tools/metadata/AnalyseMetaForm

...and it seemed to be working but I was struggling to use the correct code to get my string to display in the content, and then I saw your post with..

content="<?= $meta_description ?>"/>

Open in new window

I would never have tried enclosing in '<?' so many thanks.

Thanks also for the simplified code, tidies it up a bit.

Enjoy the rest of the day.

Adam
Chris StanyonWebDevCommented:
No worries Adam,

Wrapping it in <?= $x ?> is just the shortcut code for <?php echo $x ?>

Because you're needing to output a PHP variable into your HTML, you need to echo it out, so either:

content="<?= $meta_description ?>"/>

or

content="<?php echo $meta_description ?>"/>

They're the same thing :)

FYI - you shouldn't have a closing slash on a meta tag, unless you are coding in XHTML (more than likely not), so this:

<meta name="x" content="x">

rather than

<meta name="x" content="x" />
AdamTrying to learn phpAuthor Commented:
Ah, thanks. That makes sense.

Cheers.

Adam
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.