cakephp logo

Setting meta tag descriptions and keywords under CakePHP 1.2

Deprecated: This article has been updated, please refer to the page seo-meta-tags-under-cakephp-1-2

The following outlines a very simple method for setting meta tag descriptions and keywords in your views.

Add the following to your <head> section of your view/layout/default.ctp file.

Show Plain Text
PHP code
  1. <head>
  2.   <?php if (isset($meta_description) && !empty($meta_description)) { ?>
  3.     <meta name="description" content="<?php echo $meta_description; ?>" />
  4.   <?php } ?>
  5.   <?php if (isset($meta_keywords) && !empty($meta_keywords)) { ?>
  6.     <meta name="keywords" content="<?php echo $meta_keywords; ?>" />
  7.   <?php } ?>
  8.   ...
  9. </head>

Then at the top of your view files you can set your specific meta descriptions and keywords.

Show Plain Text
PHP code
  1. <?php
  2.   $this->pageTitle = "your unique page title");
  3.   $this->set("meta_description", "your description goes here");
  4.   $this->set("meta_keywords", "your,comma,separated,keywords,go,here");
  5. ?>

For pages dynamically generated you could simply set the meta description and keywords from your resultant array query. For example if my returned Post array had a metadesc and metakeys field I could set the description and keywords dynamically as follows:

Show Plain Text
PHP code
  1. <?php
  2.   $this->pageTitle = $post['Post']['title']);
  3.   $this->set("meta_description", $post['Post']['metadesc']);
  4.   $this->set("meta_keywords", $post['Post']['metakeys']);
  5. ?>

Related

Filed under: Cakephp  Tags: Seo

2 Responses to “Setting meta tag descriptions and keywords under CakePHP 1.2”

Or use the html helper like this on your layout :D echo $html->meta('keywords',$meta_keywords); echo $html->meta('description',$meta_description);

Yes, this article is deprecated as outlined at the start of the article. Your code example is covered in the update to this article seo-meta-tags-under-cakephp-1-2

Sorry, comments have been closed for this post.