Reputation: 39
Im trying to improve my SEO, one way of doing this is to include a description tag that contains a description of the page content, this can be shown in the Google Search Results.
The issue I'm having is that the needs to be in the site header, however I query my database in the site body, I don't want a generic meta description, I want it to update dynamically dependent on the current page. My site is a camera comparison site, so for example I would need to pass the brand name, model name and a few specifications up to the meta...
I had the same issue with the tag as I want my page titles to include the product name, I got round the issue by using a buffer, allowing me to pass the model name up to the title.
header.php:
<title>%TITLE%</title>
compare.php
ob_start();
include_once 'includes/header.php';
$buffer_title=ob_get_contents();
buffer_title=str_replace("%TITLE%", "Specced | " . $brand['brand'] . " " . $model['model'] ,$buffer_title);
echo $buffer_title;
ob_end_clean();
Using this code, I was able to query the brand name and model name based off the page ID and pass the details up to the title... Im thinking I can do the same with the META Description tag.
I understand a META tag cannot be in the site body, So my question is, I would like to be able to pass both the meta details and title details up to the page header using the object method I showed above.
header.php:
<title>%TITLE%</title>
<meta name="Description" content="%META%">
compare.php:
ob_start();
include_once 'includes/header.php';
$buffer_title=ob_get_contents();
buffer_title=str_replace("%TITLE%", "Specced | " . $brand['brand'] . " " . $model['model'] ,$buffer_title);
echo $buffer_title;
$buffer_meta=ob_get_contents();
buffer_meta=str_replace("%META%", "some dynamic meta description" ,$buffer_meta);
echo $buffer_meta;
ob_end_clean();
The issue I'm having with this code is that it includes the header file twice, you can see this on my site here:
http://cameras.specced.co.uk/compare.php?compare_1=129
I would love to know how to pass both the meta and title details up to the header using the ob_start method. Thankyou
Upvotes: 1
Views: 855
Reputation: 198214
This is a common design problem with PHP scripts that work with a header and footer.
<header>
<body>
<footer>
Technically you first need to gather the data so you can output it. But with your existing design, you first start the output (buffered or not), then you do the processing and then you realize that you started the output too early :)
You can separate the output from the processing, that is you turn the body into a body template and you do the processing up front.
For that you need to declare template variables you use for title and description (next to the variables which you use in each specific body template, but these can vary more while the header variables are fixed across all header templates).
[processing]
|
|
variables
|
\|/
<header>
<body>
<footer>
If you follow this order, you don't need to write your own find/search/replace code, you can just output variables within your templates. It is only a question of execution order.
In a program, output operations normally come last (input -> processing -> output; IPO).
Upvotes: 0