1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Meta Tags in Php.... how do you do it?

Discussion in 'PHP & Perl' started by johnrapp, Feb 1, 2012.

Tags:
  1. johnrapp

    johnrapp Registered Member

    Joined:
    May 4, 2011
    Messages:
    72
    Likes Received:
    8
    Occupation:
    CEO @ TCB
    Location:
    Greenwood, IN
    Home Page:
  2. hameem

    hameem Elite Member

    Joined:
    Jul 21, 2011
    Messages:
    1,880
    Likes Received:
    985
    Location:
    221B Baker Street
    They don't need to tell you how to deal with PHP because meta tag is a HTML tag. You just put the meta tag as a normal HTML tag. Anyone who understands how PHP works, should know that.
     
  3. johnrapp

    johnrapp Registered Member

    Joined:
    May 4, 2011
    Messages:
    72
    Likes Received:
    8
    Occupation:
    CEO @ TCB
    Location:
    Greenwood, IN
    Home Page:
    look what this guy says to do:

    http://www.phpfreaks.com/forums/index.php?topic=300653.0

    my header file contains the <head> but I would like to have different meta tags displayed depending on the page being displayed. I know you could insert <head> into each file and be done with it, but that may drive me nuts as the site grows. So I'm trying to see how I can do it differently

    So far I can do this with php
    <?php

    $meta_title
    ="Site title goes here";

    $meta_heading="Site heading info goes here";

    $meta_desc="Site Description goes here";

    $meta_keys="Site, Key, Words, Go, Here";

    ?>

    Then I can call that meta tag by inserting this in the pagesCode: [Select]
    <meta name="description" content="<?php echo $meta_desc ?>" />
    I also have Code: [Select]
    $page=" unique page name" inserted in each page.

    I know this php meta script is incomplete, but what can I do to take it to the next level? Do I need to use and if statements, and how would I?

    Thanks for all your help
     
    Last edited: Feb 1, 2012
  4. tnhomestead

    tnhomestead Regular Member

    Joined:
    Oct 9, 2011
    Messages:
    385
    Likes Received:
    253
    Location:
    Tenneessee USA
    Home Page:
    What you would do after that is store all the page information in a database, then pull the different meta tags for each page based on page title (or another unique page identifier) and then display them. Otherwise just insert the meta tags in the php code. Couple ways to do that.
    1. <?php some code here add
    ?>
    meta tags go here -- ie straight html
    at end of html
    <?php
    this breaks you out of php and displays html
    or option 2 is to use a echo type statement
    echo 'meta tags go here';
    which will display the tags as they are output from php
     
  5. hameem

    hameem Elite Member

    Joined:
    Jul 21, 2011
    Messages:
    1,880
    Likes Received:
    985
    Location:
    221B Baker Street
    So if I understand you correctly, you want to set different meta tags based on different (and unique) page names and you don't want to use database either.

    If that's the case, you can do the following.

    1. Make a separate PHP page containing all the meta tags you want. Let's say it's "MetaTags.php".

    2. Make a if-else (or even better, a switch) statement mapping different meta tags to different unique page names. Put the whole thing in a function, lets say giveMeMeta($pagename). You get the picture, if you feed this function with your page's name, it'll return the meta tags.

    3. Import MetaTags.php to every other php page you create.

    4. Call that function from each of your PHP pages. Something like this:
    $meta_tags = giveMeMeta($pagename);
    And then set the $meta_tags where applicable.
     
  6. chevet

    chevet Jr. VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    101
    Likes Received:
    30
    Hameem's giveMeMeta function example:
    PHP:
    <?php
    function giveMeMeta($pagename) {
        
    $meta = array();
        switch (
    $pagename) {
            case 
    'home':
                
    $meta['title'] = 'Home page'
                
    $meta['heading'] = 'home sweet home';
                
    $meta['desc'] = 'my home page';
                
    $meta['keys'] = 'home, page, main';
                break;
            case 
    'home2':
                
    $meta['title'] = 'Home page2'
                
    $meta['heading'] = 'home sweet home2';
                
    $meta['desc'] = 'my home page2';
                
    $meta['keys'] = 'home, page, main2';
                break;
            case 
    'home3':
                
    $meta['title'] = 'Home page3'
                
    $meta['heading'] = 'home sweet home3';
                
    $meta['desc'] = 'my home page3';
                
    $meta['keys'] = 'home, page, main3';
                break;
        }
        return 
    $meta;
    }
    ?>
    usage:
    if you have this function in separate file you must add:
    PHP:
    include('filename.php');
    PHP:
    $pagename 'contact';
    $meta giveMeMeta($pagename);
    html:
    Code:
    <meta name="description" content="<?php echo $meta['desc'] ?>" />
     
    • Thanks Thanks x 2
  7. dogwalker

    dogwalker Newbie

    Joined:
    Sep 3, 2011
    Messages:
    7
    Likes Received:
    4
    if you use database, you save meta data in database, and retrieve them from db respectively.
    in db you can make table to store articles like this one:
    - article_id
    - article_title
    - article_summary
    - article_text
    - article_tags

    Inside <title> tags goes the title of the article, in <meta description> you can put summary of the article. There is no need to create particular columns for <meta title> or <meta description> tags because they are redundant, but in you can do that as well, it depends on you application.
     
  8. wulfgen

    wulfgen Newbie

    Joined:
    Jan 26, 2012
    Messages:
    3
    Likes Received:
    0
    I was following along on this and was wondering if:
    (all this is supposition)

    in Wordpress for example - you could add to the header template an include like addmeta.php - in that file you would have pointers to the metas of keywords and description. Maybe $desc $keys

    Those would be populated from the entries you put into the page edit area in wordpress prior to the actual page content $keys=dog, fish, cat $desc=My cool Dog/Fish/Cat Collection.

    Does that sound way too whacky?
     
  9. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,879
    Likes Received:
    1,932
    I am not a big WP user but I am sure that there will be a plugin out there that sets your meta keywords to your blog post keywords already. If not then there is probably a market here for long tail keywords!
     
  10. randomnumbers

    randomnumbers Newbie

    Joined:
    Jun 18, 2011
    Messages:
    20
    Likes Received:
    2
    If you're using WP there are tons of addons which handles meta tags, "all in one seo" for example.
     
  11. wulfgen

    wulfgen Newbie

    Joined:
    Jan 26, 2012
    Messages:
    3
    Likes Received:
    0
    Yep found that one - I was just querying for my own edification
     
  12. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Ultimately, I would create a MySQL database and manage all my tags from a backend system. Nevertheless, for a PHP solution, I stick to arrays:

    Code:
    <?
    // Sets up meta data array
    $metaInfoArr = array(
                        "index.php" => array(
                                            "meta_title" => "My Home Page",
                                            "meta_description" => "This is my home page.",
                                            "meta_keywords" => "home, page, house, casa"
                                       ),
                        "about.php" => array(
                                            "meta_title" => "About Us",
                                            "meta_description" => "This has info about us.",
                                            "meta_keywords" => "about, quien es"
                                       )
                   );
    
    // Gets the current file name
    $thisScriptArr = explode('/', $_SERVER["SCRIPT_NAME"]);
    $thisFile = $break[count($thisScriptArr) - 1]; 
    ?>
    <html>
    <head>
         <title><?=$metaInfoArr[$thisFile]["meta_title"];?></title>
         <meta name="description" content="<?=$metaInfoArr[$thisFile]["meta_description"];?>">
         <meta name="description" content="<?=$metaInfoArr[$thisFile]["meta_keywords"];?>">
    </head>
    <body>
    </body>
    </html>