We help IT Professionals succeed at work.

Meta tag depending of selected and title

lenamtl
lenamtl asked
on
Hi,

I'm aware about Google ignore most of the time, meta tag and use content. (This is not the point here)

I'm working on an existing web site, not created by me. I need a quick solution, I guess with variables.

The website construction: (no known template system)

index.html which is presentation page with language selection

 menu, content, footer several content pages that are embedded by index.php

What I need to do only for those 2 pages welcome_en.html and welcome_fr.html (these pages are embedded so no header possible on these page) to have different page title (browser title) and different META tag.

Any solution is welcome

Thanks

extra information

Language detection code on index.php:

<?php
$lang = $_GET['lang'];
$page = $_GET['page'];
if ($_GET['page'] == "" || !$_GET['page']) {
$page = "welcome";
}
if ($_GET['lang'] == "" || !$_GET['lang']) {
$lang = "_fr";
}
?>

Open in new window



for the embeded menu, footer ect like this one

<?php include "menu.php";
  ?>

Open in new window


for the embedded content
<?php
        //echo "$page$lang.html";
        $lang = preg_replace('/[^a-z0-9_ ]/i', '', $_GET['lang']);
        $page = preg_replace('/[^a-z0-9_ ]/i', '', $_GET['page']);
        include $page . $lang . ".html";
  ?>

Open in new window


The header info Meta tag and title are in index.php for all pages with no variables.

PS II'm not using any CMS or Framework or template system and don't want to use them.
Comment
Watch Question

Andyc75Software Architect
Top Expert 2010

Commented:
HI lenamtl,

here is an example of how to do this.

Note: I changed the logic for getting the the $lang  and $page values.  You need to make sure you check that the $_GET variables exists before you use them, or you will get errors.

if no parameters are supplied I default it to _fr and welcome.

index.php?lang=_fr&page=welcome
index.php?lang=_en&page=welcome
index.php


<?php
//get the page parameters from the url (default is _fr, and welcome)
$lang = (isset($_GET['lang'])) ? $_GET['lang'] : "_fr";
$page = (isset($_GET['page'])) ? $_GET['page'] : "welcome";

//Add this code after the <head> tag
if($lang == "_fr"){
  echo '<title>Welcome French</title>' . PHP_EOL;
  echo '<meta name="Description" content="Welcome French" />' . PHP_EOL;
  echo '<meta name="Keywords" content="French Keywords" />' . PHP_EOL;
}else{
  echo "<title>Welcome English</title>" . PHP_EOL;
  echo '<meta name="Description" content="Welcome English" />' . PHP_EOL;
  echo '<meta name="Keywords" content="English French" />' . PHP_EOL;
}

?>

Open in new window

Hi,

Ok I'll give it a try and get back to you.

Thanks
Commented:
You will have to take the page and language into consideration in order to show the relevant information. You do not want to show French keywords in English version and vice versa. So it's better and safe to do a check explicitly and code appropriately.

As such, I have attached the code which achieves the above functionality. Also the code "assumes" that the title for the index pages in both the versions for the index.php pages is: welcome. If you have other title, then just simply replace it in the code.

Try it out and let me know if it works.
<?php 
if($page == "welcome" && ( $lang == "_en" || $lang == "") )
{
  echo '<title>English Title</title>';
  echo '<meta name="Keywords" content="English Keywords" />';
  echo '<meta name="Description" content="English Description" />';  
}
elseif($page == "welcome" && $lang == "_fr")
{
  echo '<title>French Title</title>';
  echo '<meta name="Keywords" content="French Keywords" />';
  echo '<meta name="Description" content="French Description" />';  
}
?>

Open in new window

Commented:
Any luck?
Hi mepro,

It is working perfectly.

Thanks

Commented:
Thanks for letting me know. I am glad I was able to help you out.

Good day!