-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Meta tags
Meta_tags is a flexible PHP4-compatible library for CodeIgniter, easening the process of creating meta tags for your pages. Often when you're juggling with lots of different views with different access levels(differing ROBOTS tags) and various keywords for SEO, it can be beneficial to let CodeIgniter handle the manual labor of meta tags. This is made easy by Meta_tags.
Here follows prototypes for usage and configuration. Scroll to the bottom for a full function reference.
```php$config['doctype'] = 'xhtml'; $this->load->library('meta_tags'); $this->meta_tags->initialize($config); $this->meta_tags->set_meta_tag('author', 'Per Sikker Hansen'); $this->meta_tags->add_robots_rule('NOINDEX'); $this->meta_tags->add_keyword('php'); echo $this->meta_tags->generate_meta_tags();
<h4>Configuration:</h4>
<p>Configurations can be passed to the library constructor or the initialize() method if you are autoloading the library, and default values can optionally be stored in config.php</p>
<p>Configurations stored in config.php should follow this prototype:</p>
```php
$config['meta_tags']['doctype'] = 'xhtml';
$config['meta_tags']['tags'] = array('tagname'=>'tagcontent', 'another tag'=>'some other content');
$config['meta_tags']['robots'] = array('NOINDEX', 'FOLLOW', 'NOARCHIVE');
$config['meta_tags']['keywords'] = array('great', 'php', 'framework');
While configurations passed to the initialize() method or the constructor follow a slightly different pattern:
```php$config['doctype'] = 'xhtml'; $config['tags'] = array('tagname'=>'tagcontent', 'another tag'=>'some other content'); $config['robots'] = array('NOINDEX', 'FOLLOW', 'NOARCHIVE'); $config['keywords'] = array('great', 'php', 'framework');
<h3>Installation</h3>
<p>Installation is easy. Copy the code below into a file called meta_tags.php and place it in application/libraries on your server. The code is fully documented, so you shouldn't have to worry too much about how it works.</p>
<h3>The library</h3>
```php
<?php
/**
* A meta tag generation library for CodeIgniter.
*
* Copyright (c) 2009 Per Sikker Hansen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Usage example:
* -------------------
* $config['doctype'] = 'xhtml';
* $this->load->library('meta_tags');
* $this->meta_tags->initialize($config);
* $this->meta_tags->set_meta_tag('author', 'Per Sikker Hansen');
* $this->meta_tags->add_robots_rule('NOINDEX');
* $this->meta_tags->add_keyword('php');
* echo $this->meta_tags->generate_meta_tags();
* -------------------
* Optionally you can store a default configuration array in config.php with this prototype:
* $config['meta_tags']['doctype'] = 'xhtml';
* $config['meta_tags']['tags'] = array('tagname'=>'tagcontent', 'another tag'=>'some other content');
* $config['meta_tags']['robots'] = array('NOINDEX', 'FOLLOW', 'NOARCHIVE');
* $config['meta_tags']['keywords'] = array('great', 'php', 'framework');
*
* @author Per Sikker Hansen <[email protected]>
*/
class Meta_tags {
var $CI;
var $doctype;
var $tags;
var $keywords;
var $robots;
/**
* Class constructor with optional parameter, which calls the initialize() method
* @param $config array optional array containing configuration
*/
function Meta_tags($config = array())
{
$this->CI =& get_instance();
$this->initialize($config);
}
/**
* Initializes the library with the configuration given either in a parameter or in config.php
* @param $config array optional array containing configuration and global default meta tags
*/
function initialize($config = array())
{
if(empty($config))
{
$config = $this->CI->config->item('meta_tags');
if(!$config)
$config = array();
}
if(isset($config['doctype']))
{
$this->doctype = $config['doctype'];
}
else
{
$this->doctype = 'xhtml'; //if no doctype is given, default to XHTML
}
if(isset($config['tags']))
$this->tags = $config['tags'];
if(isset($config['keywords']))
$this->keywords = $config['keywords'];
if(isset($config['robots']))
$this->robots = $config['robots'];
}
/**
* Sets the doctype for which the tags will be generated
* @param $doctype string choices are 'xhtml' and 'html'
*/
function set_doctype($doctype)
{
$this->doctype = $doctype;
}
/**
* Sets a meta tag with name and content
* @param $name string
* @param $content string
*/
function set_meta_tag($name, $content)
{
$this->tags->$name = $content;
}
/**
* Removes a meta tag
* @param $name string name of the tag
*/
function unset_meta_tag($name)
{
unset($this->tags->$name);
}
/**
* Adds a unit to the keyword array
* @param $keyword string
*/
function add_keyword($keyword)
{
$this->remove_keyword($keyword);
$this->keywords[] = $keyword;
}
/**
* Searches the keywords array and removes the given keyword
* @param $keyword string
*/
function remove_keyword($keyword)
{
$this->_search_and_remove($keyword, $this->tags);
}
/**
* Adds a rule to the robots array
* @param $rule string
*/
function add_robots_rule($rule)
{
$this->remove_robots_rule($rule);
$this->robots[] = $rule;
}
/**
* Searches the robots array and removes the given rule
* @param $rule string
*/
function remove_robots_rule($rule)
{
$this->_search_and_remove($rule, $this->robots);
}
/**
* Library-only function for searching and removing
* @param $needle string
* @param $haystack array
*/
function _search_and_remove($needle, $haystack)
{
$key = array_search($needle, $haystack);
if($key)
{
unset($haystack[$key]);
}
}
/**
* Passes the contained data to private functions for processing
* @return string the compiled meta tags for insertion into your view
*/
function generate_meta_tags()
{
switch(strtolower($this->doctype))
{
case 'xhtml':
$output = $this->_generate_xhtml_meta_tags();
break;
case 'html':
$output = $this->_generate_html_meta_tags();
break;
}
return $output;
}
/**
* Generates meta tags following the XHTML conventions
* @return string the compiled meta tags for insertion into your view
*/
function _generate_xhtml_meta_tags()
{
$output = "\n";
if(!empty($this->tags))
{
foreach($this->tags as $name=>$content)
{
$output .= '<meta name="'.$name.'" content="'.$content.'" />'."\n";
}
}
if(!empty($this->keywords))
$output .= '<meta name="keywords" content="'.implode(',', $this->keywords).'" />'."\n";
if(!empty($this->robots))
$output .= '<meta name="robots" content="'.implode(',', $this->robots).'" />'."\n";
return $output;
}
/**
* Generates meta tags following the HTML conventions
* @return string the compiled meta tags for insertion into your view
*/
function _generate_html_meta_tags()
{
$output = "\n";
if(!empty($this->tags))
{
foreach($this->tags as $name=>$content)
{
$output .= '<META NAME="'.$name.'" CONTENT="'.$content.'">'."\n";
}
}
if(!empty($this->keywords))
$output .= '<META NAME="keywords" CONTENT="'.implode(',', $this->keywords).'">'."\n";
if(!empty($this->robots))
$output .= '<META NAME="robots" CONTENT="'.implode(',', $this->robots).'">'."\n";
return $output;
}
}
/* End of file meta_tags.php */
/* Location: ./system/application/libraries/meta_tags.php */
The methods of the library are explained here, with examples and corresponding configuration keys.
The initializing method, called by the constructor when the library is loaded and can be optionally called by a controller. This is useful if you're autoloading the library but need to pass custom configurations. The method takes one optional parameter, $config, which should be an array conforming to the prototype stated at the top of this document. If no array is passed, it will get the configurations from config.php (if any)
```php$this->meta_tags->initialize(); //no config array $this->meta_tags->initialize(array('doctype'=>'xhtml'));
<h4>set_doctype()</h4>
<p>Changes the doctype. The doctype is either 'xhtml' or 'html' and is used by the library to determine how the meta tags should look, enabling generation of valid (x)html at your leisure. The default doctype is XHTML.</p>
```php
$this->meta_tags->set_doctype('html');
Sets a meta tag. If the tag exists, it is overwritten.
```php$this->meta_tags->set_meta_tag('author', 'Per Sikker Hansen');
<h4>unset_meta_tag()</h4>
<p>Removes a meta tag.</p>
```php
$this->meta_tags->unset_meta_tag('author');
Adds a keyword to the keywords array if it does not exist.
```php$this->meta_tags->add_keyword('CodeIgniter');
<h4>remove_keyword()</h4>
<p>Removes a keyword from the keywords array.</p>
```php
$this->meta_tags->remove_keyword('CodeIgniter');
Adds a rule for ROBOTS to follow(or nofollow...)
```php$this->meta_tags->add_robots_rule('ALL');
<h4>remove_robots_rule()</h4>
<p>Removes a rule from the robots array.</p>
```php
$this->meta_tags->remove_robots_rule('ALL');
Runs through the arrays built by configuration as well as methods, implodes and wraps in (x)html, returning the compiled meta tags as a string, ready to echo from a view.
```php$data['meta_tags'] = $this->meta_tags->generate_meta_tags();