PHP Buffer To Pass The META Description And TITLE To The Site Header
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.
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.
<title>%TITLE%</title> <meta name="Description" content="%META%">
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:
I would love to know how to pass both the meta and title details up to the header using the ob_start method. Thankyou
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).
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?