Dynamic title and meta tag generator

Discussion in 'PHP' started by ajuas, Aug 2, 2010.

  1. #1
    Its difficult to create different title and meta for each page of a website, but we have to add custom meta and title for few pages. Title of inner pages should be created dynamically depend on the page content.

    Someone please share me a PHP code to create this.


    thanks in advance
     
    ajuas, Aug 2, 2010 IP
  2. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #2
    its a bit complicated.
    u need to use eregi and if statements like this:
    
    <?php
    if (eregi("page1.php",$_SERVER[REQUEST_URI]) //check if page1.php is in the requested url
    {
    echo "<meta name='keywords' content='your keywords'><title>Title For page1.php</title>";
    }
    else if (eregi("page2.php",$_SERVER[REQUEST_URI]) //check if page2.php is in the requested url
    {
    echo "<meta name='keywords' content='your keywords'><title>Title For page2.php</title>";
    }
    else //if both the page1 and page2 are not in url.
    {
    echo "<meta name='keywords' content='your keywords'><title>Title For pagename.php</title>";
    }
    ?>
    
    PHP:
    you need to have some knowledge of php for this.
    on my site i also use this coding ;)
     
    arpit13, Aug 2, 2010 IP
  3. ajuas

    ajuas Greenhorn

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    I think I have to create a php file to include this code, and add some calling fn on the title to call this. Please gimme advice, i am new to php.
     
    ajuas, Aug 2, 2010 IP
  4. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #4
    if u have queries related to this coding pm me.
     
    arpit13, Aug 2, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Do you have a main header.php file with meta instructions ?

    What would you want the title to be, maybe some story/news/product ?

    If you post up what you have including a sample pages php, it would be easier to advise you further.
     
    MyVodaFone, Aug 2, 2010 IP
  6. ajuas

    ajuas Greenhorn

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    This is a very simple PHP snippet that will set $title, $meta_keywords and $meta_description for use in your page header. You'll need to create meta_tags.txt which will use this format:

    page_name.html|page title|meta tags|meta description
    page_name2.html|page title2|meta tags for page 2|meta description for page 2

    To use the code either save it at the top of your header file or save it as get_meta.php and add this line of code to your header:


    <?php include('get_meta.php'); ?>

    <title><?php print $title; ?></title>
    <meta name="keywords" content="<?php print $meta_keywords; ?>">
    <meta name="description" content="<?php print $meta_description; ?>">





    <?php

    $database = 'meta_tags.txt';
    $meta_db = fopen($database, 'r');

    $page = $_SERVER['SCRIPT_NAME'];
    $page = substr($page, 1);

    while($data = fgetcsv($meta_db, 9000, '|'))
    {
    if($data[0] == $page)
    {
    $title = $data[1];
    $meta_keywords = $data[2];
    $meta_description = $data[3];
    }
    }

    ?>



    I would like to whether it is working or not, someone please help me..
     
    ajuas, Aug 7, 2010 IP