Search code examples
phphtmlxmlmeta

Dynamically generated meta tags


I'm looking for a simple straightforward solution for dynamic meta description in a header.php. What I have so far works but I'm not satisfied with the amount of lines, I think this can be done with less code and a nicely structured XML file.

What I have so far:

    <?php
        $full_name = $_SERVER['PHP_SELF'];
        $name_array = explode('/',$full_name);
        $count = count($name_array);
        $page_name = $name_array[$count-1];
    ?>
    <meta name="description" content="
    <?php echo ($page_name=='index.php')?'De Nummer 1 in WebHosting, WordPress Hosting en vps infrastructuur. Professionele support gemakkelijk controlepaneel en vele domein extensies':'';?> 
    <?php echo ($page_name=='about.php')?'De Nummer 1 in WebHosting, WordPress Hosting en vps infrastructuur. Professionele support gemakkelijk controlepaneel en vele domein extensies':'';?> 
    <?php echo ($page_name=='webhosting.php')?'web hosting':'';?>
    <?php echo ($page_name=='website_builder.php')?'builder':'';?>
    <?php echo ($page_name=='wordpress_hosting.php')?'WordPress Hosting':'';?>
    <?php echo ($page_name=='wordpress_builder.php')?'WordPress Builder':'';?>
    <?php echo ($page_name=='domain_search.php')?'Zoek je Domeinnaam':'';?>
    <?php echo ($page_name=='domain_transfer.php')?'Verhuisje domeinnaam':'';?>
    <?php echo ($page_name=='contact.php')?'Contacteer ons':'';?>
    <?php echo ($page_name=='support.php')?'Support':'';?>" 
...
    />

What I am looking for

<?php
    $full_name = $_SERVER['PHP_SELF'];
    $name_array = explode('/',$full_name);
    $count = count($name_array);
    $page_name = $name_array[$count-1];
?>
<meta name="description" content="
<?php echo ($page_name=='   $page   ')?'  $content   ':'';?> 
/>

$page is supposed to be the pagename.php and $content is the meta description for that particular page. There are 20+ pages that are dynamically generated with PHP that all need a particular description, keywords,... I was thinking to put the descriptions and keywords in an XML file and call, depending on page, the right content. How would I do this? I'm quite new to PHP and I spend the last 4 hours trying to solve this... I also tried some tutorials but nothing really suits my needs.


Solution

  • There is one simple solution for that.

    Step 1

    Create meta.ini file and write your file name and meta description like below

    index.php="your meta description"
    wordpress-host.php="your meta description"
    

    Step 2

    <?php
    $meta = array();
    $metaFile = 'your dir path/meta.ini';
    if(file_exists($langFile)){
        $meta = parse_ini_file($metaFile);
    }
    ?>
    

    Step 3

    <?php
        $full_name = $_SERVER['PHP_SELF'];
        $name_array = explode('/',$full_name);
        $count = count($name_array);
        $page_name = $name_array[$count-1];
        if(isset($meta[$page_name])){
            echo '<meta name="description" content="'.$meta[$page_name].'"/>';
        }
    ?>