Hi guys!
I need help with meta tags on my website!
Look on all intire website i have more then 20 pages, and im using only one head.php and footer.php files.

So i want to have a meta tags for each page, without dupicating the head.php, and i'm really struggling. is there a way to do this without duplicating the head.php

I hope get some repply from you guys!

Member Avatar


Sure, there are a hundred different ways to do it.
You could have a config_page.php file:

    case 'home':
        $metaDesription= "blah blah";
        $metaTitle= "kdKJVD";
case 'about':
        $metaDesription= "blah blah2";
        $metaTitle= ";llwjvb";
case 'contact':
        $metaDesription= "blah blah3";
        $metaTitle= "lskbvlkv";

So you could do this:

$page = ... //get this from your url dynamically or hardcode it.
require "includes/config_page.php";
require "header.php";
//rest of page if in plain html - or require the page itself if in a pages directory
require "footer.php";

Your header could look like this...

<meta name="description" content="<?=metaDescription?>" />


Else you could just hard code the variables into the top of each page individually.

$metaTitle = 'skvblwkv';
$metaDescription = 'dklWKJDKJ';
require "header.php";

However, we're fast running into templating territory. If you have a lot of reusable content, you may want to start thinking about a template engine, like Smarty, Twig or RainTPL or a hundred others. Similarly, if you have a lot of processing involved with each page, you could start looking at an MVC structure or even a PHP framework, but these may be overkill - depends on the complexity of your site.

commented: good +1

Thanks Diafol!
Really i use Twig for all my rojects, but this project i'm just doing maintance, the applicatiom started with different guys, and they used some cms to build everything, so is just native php there and html.
so thanks i'll use this way with switch page, i think is good solution i have for now!