Generate dynamic meta-tags based on URL
You are on the right track :-)
Do it in following way:
<TITLE><?php echo $metas['index.php']['title']; ?></TITLE>
Script name you can get it from the $_SERVER super global array/variable.
Also from your config.php file remove last 3 lines, you don't need them.
Good luck, with PHP make sure you get good knowledge about playing with array. That's key.
EDIT:
$page = 'index.php';
if ( isset( $_GET['action'] ) && $_GET['action'] != "" )
{
$page = $_GET['action'];
}
<TITLE><?php echo $metas[$page]['title']; ?></TITLE>
EDIT: (on 27th May 2012)
I think this is pretty much everything:
Your code in config.php
<?php
$meta['INDEX']['title'] = "Home page";
$meta['INDEX']['keywords'] = "kwd1, kwd2, kwd3";
$meta['INDEX']['description'] = "Home description";
$meta['SIGNUP']['title'] = "Sign up..!";
$meta['SIGNUP']['keywords'] = "kwd1, kwd2, kwd3";
$meta['SIGNUP']['description'] = "Sign up description";
$meta['ABOUT']['title'] = "About Company";
$meta['ABOUT']['keywords'] = "kwd1, kwd2, kwd3";
$meta['ABOUT']['description'] = "About company description";
Your code in any of your application page/screen (.php files):
<?php
include "config.php";
// Choice 1: if you want to specify page hardcoded at each page then you can say $page = 'ABOUT' for about-us page and $page = 'INDEX' for home page etc.
$page = 'ABOUT';
if( strtoupper($_SERVER['REQUEST_URI'], $page) !== false )
{
$title = $meta[$page]['title'];
$keywords = $meta[$page]['keywords'];
$description = $meta[$page]['description'];
}
// Now you have your meta - use it the way you want
echo $title;
// Choice 2: If you want to make it little more dynamic
// Here you don't need to define any hardcoded variable at page level as everything will be considered from the URL being requested
$page_index = array_keys($meta);
foreach($page_index as $page)
{
if ( strpos( strtoupper($_SERVER['REQUEST_URI']), $page ) !== false)
{
$title = $meta[$page]['title'];
$keywords = $meta[$page]['keywords'];
$description = $meta[$page]['description'];
break;
}
}
// Now you have your meta - use it the way you want
echo $title;
?>
Genesis
Updated on May 27, 2020Comments
-
Genesis about 4 years
I'm trying to ASSIGN dynamically exclusive meta-tags for each page:
i.e: url.com/index.php?action=signup
Header - Signup Title
Keywords - Signup Meta Keywords
Description - Signup Description
url.com/index.php?action=about
Header - About Title
Keywords - About Meta Keywords
Description - About Description
You got the point.
I'm using arrays...but didn't figured yet HOW TO ASSIGN each array to each page.
CONF.PHP
<?php $metas = array( 'index.php' => array( 'header' => 'Home Title', 'keywords' => 'Home Meta Keywords', 'description' => 'Home Meta Description' ), 'signup' => array( 'header' => 'Signup Title', 'keywords' => 'Signup Meta Keywords', 'description' => 'Signup Meta Description' ), 'about' => array( 'header' => 'About Title', 'keywords' => 'About Meta Keywords', 'description' => 'About Meta Description' ) ); ?>
INDEX.PHP
<TITLE><?php echo $metas['title']; ?></TITLE> <meta name="description" content="<?php echo $metas['description']; ?>" > <meta name="keywords" content="<?php echo $metas['keywords']; ?>" >
How to ASSIGN those values to each page???
-
Genesis about 12 yearsBut, how can I put index.php array at index.php if all pages are loaded inside it? SIGNUP - index.php?action=signup / ABOUT - index.php?action=about...etc
-
Genesis about 12 yearsI used switch method to assign values. But now I have problems for assigning variables get from a MySQL select...how can I do that? Ex: url.com/news/$id/$title - How to assign meta tags in such case?
-
deej about 12 yearsIn that case you have to search for specific word on your full URL using strpos($haystack, $findme) method. If you find news you should use those meta tags and so on. So you can do it 2 way 1) use action or 2) if action is blank look for your keyword using strpos and get the right meta tag from the config.
-
Genesis about 12 yearsCan you be more specific please?
-
deej about 12 years@Darkeden I have added an example to support my comment. Please check.
-
deej about 12 years@Darkeden did that made more sense, after I updated answer with updated code/example?