Jump to content

Smarty SEO plugin: meta_tag


Recommended Posts

Part 2 of 3

 

File name: outputfilter.meta_tag.php

 

<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/

/**
* Smarty meta_tag outputfilter plugin
*
* File:     outputfilter.meta_tag.php
* Type:     outputfilter
* Name:     meta_tag
* Date:     Apr 03, 2009
* 
* Purpose:  Update or replace meta tags and page title using 
*           content contained within {meta_tag}{/meta_tag}
*           in a Smarty template.
* 
*              Requires block.meta_tag.php to enable {meta_tag} usage.
* 
* Install:  Add outputfilter.meta_tag.php to the Smarty plugin 
*           directory along with block.meta_tag.php, then call:
* 
*           <code>
*           $smarty->load_filter('output','meta_tag');
*           </code>
* 
*           The above function call should appear BEFORE using 
*           $smarty->display or $smart->fetch.
* 
* @author   Milan Hawkins <milan at include-digital dot com>
* @copyright Include - http://www.include-digital.com
* @link http://blog.include-digital.com/2009/04/03/smarty-seo-plugin-meta_tag
* @version  1.0.1
* @param string
* @param Smarty
*/
function smarty_outputfilter_meta_tag($content, &$smarty)
{
   if (is_null($content)) {
       return;
   }

   // fetch current title tag(s)
   preg_match_all('/\<title\>(.*)\<\/title\>/i',$content,$matches,PREG_SET_ORDER);

   // use first instance of title tag for original title, otherwise default to empty string
   isset($matches[0][1]) ? $title =  $matches[0][1] : $title = "";

   // delete extraneous title tags
   if (count($matches) > 1) {
       array_shift($matches);
       foreach ($matches as $match) {
           $content = str_replace($match[0],'',$content);
       }                
   }    

   $valid_meta = array('keywords','description','author');

   foreach ($valid_meta as $v) {

       // fetch current meta
       preg_match_all('/\<meta\s{0,}+name=["\']'.$v.'["\']\s{0,}+content=[\"|\'](.*)["\']\s{0,}+\/\>/i',$content,$matches,PREG_SET_ORDER);

       // use first instance of meta, deleting duplicates
       isset($matches[0][1]) ? $$v =  $matches[0][1] : $$v = "";

       // delete extraneous meta tags
       if (count($matches) > 1) {
           array_shift($matches);
           foreach ($matches as $match) {
               $content = str_replace($match[0],'',$content);
           }                
       }

   }

   // find meta_tag content added by smarty
   preg_match_all("/\<meta_tag type=\'([A-Za-z]+)\' method\=\'([A-Za-z]+)\'\>(.+)\<\/meta_tag\>\r\n/i",$content,$matches,PREG_SET_ORDER);

   // create arrays for content
   $title = array($title);
   $keywords = array($keywords);
   $description = array($description);
   $author = array($author);

   // add meta data to arrays
   foreach ($matches as $match) {
       $type = strtolower($match[1]);
       $method = strtolower($match[2]);
       $val = str_replace('"','\'',$match[3]);
       switch ($method) {
           case 'add':
               array_push($$type,$val);
               break;
           case 'replace':
               $$type = array($val);
               break;
           default:
               $smarty->trigger_error("meta_tag: unknown method '$method', must be either 'add' or 'replace'");

       }
   }

   // add meta data to page output
   foreach ($valid_meta as $v) {

       $meta = $$v;
          $meta = implode($meta);
       $meta = "<meta name=\"$v\" content=\"".$meta."\" />";
       $content = preg_replace('/\<meta\s{0,}+name=["\']'.$v.'["\']\s{0,}+content=["\'].*["\']\s{0,}+\/\>/i',$meta,$content);

   }

   // create new title tag
   $title = "<title>".implode($title," - ")."</title>";

   // insert new title tag into page    
   $content = preg_replace('/\<title\>(.*)\<\/title\>/i',$title,$content);

   // remove redundant meta_tag data added by smarty
   $content = preg_replace("/\<meta_tag type=\'[a-z]+\' method=\'[a-z]+\'\>.*\<\/meta_tag\>/i",'',$content);

   // return output     
   return $content;

}

?>

Link to comment
Share on other sites

Part 3 of 3

 

File Name: block.meta_tag.php

 

<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/

/**
* Smarty {meta_tag}{/meta_tag} block plugin
*
* File:     block.meta_tag.php
* Type:     block function
* Name:     meta_tag
* Date:     Apr 03, 2009
*
* Purpose:  Convert text block for use by the meta_tag outputfilter.
* 
* Install:  Add block.meta_tag.php to the Smarty plugin directory along 
*           with outputfilter.meta_tag.php, then call from Smarty 
*           template using:
* 
*           <code>
*           {meta_tag type='' method=''}CONTENT GOES HERE{/meta_tag}
*           </code>
* 
*           Requires parameters 'type' and 'method'
*           to function.
* 
*              Parameter 'type' must be one of the following:
* 
*              title, description, keywords, author
* 
*           Parameter 'method' must be one of the following:
* 
*           add, replace
* 
*           Setting the 'type' parameter updates the corresponding
*           meta data. The 'method' parameter dictates whether
*           existing meta data should be added to or replaced.
*           
*           Example:
* 
*           <code>
*           {meta_tag type='title' action='replace'}Hello world!{/meta_tag}
*              {meta_tag type='description' action='add'}head,shoulders,knees,toes{/meta_tag}
*           </code>
* 
*           The above example REPLACES the current page title with the text "Hello World!"
*           and ADDS "head,shoulders,knees,toes" to the keywords.
* 
* @author Milan Hawkins <milan at include-digital dot com>
* @copyright Include - http://www.include-digital.com
* @link http://blog.include-digital.com/2009/04/03/smarty-seo-plugin-meta_tag
* @version  1.0.1
* @param array $params Array of parameters 
* @param string Contents of the block
* @param Smarty Smarty instance
* @return string $content Information contained within {meta_tag}{/meta_tag} re-formatted for use by the meta_tag output filter
*/
function smarty_block_meta_tag($params, $content, &$smarty)
{
   if (is_null($content)) {
       return;
   }

   // normalize content
   $content = trim(strip_tags($content));

   // check for valid params
   foreach ($params as $_key => $_val) {

       switch (strtolower($_key)) {

           case 'type':
           case 'method':

               $$_key = (string)$_val;
               break;

           default:

               $smarty->trigger_error("meta_tag: unknown attribute '$_key'");
       }
   }

   // check type
   switch (strtolower($type)) {

       case 'title':
       case 'description':
       case 'keywords':
       case 'author':

           // output tags to screen for parsing by outputfilter
           $content =  "<meta_tag type='".$type."' method='".$method."'>".$content."</meta_tag>\r\n";
           break;

       default:
           $smarty->trigger_error("meta_tag: unknown type '$type'");

   }

   return $content;

}

?>

 

But can this be used and if so how do I /We plug it in?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated