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.
(default) 11 queries took 6 ms
NrQueryErrorAffectedNum. rowsTook (ms)
1DESCRIBE `posts`17171
2DESCRIBE `comments`11111
3DESCRIBE `tags`221
4DESCRIBE `categories`221
5DESCRIBE `posts_tags`221
6DESCRIBE `categories_posts`220
7SELECT `Post`.`id`, `Post`.`url`, `Post`.`title`, `Post`.`icon`, `Post`.`metadesc`, `Post`.`metakeys`, `Post`.`categories`, `Post`.`tease`, `Post`.`body`, `Post`.`private_body`, `Post`.`created`, `Post`.`modified`, `Post`.`status`, `Post`.`allow_comments`, `Post`.`tags`, `Post`.`hitcount`, `Post`.`hitcount_rss` FROM `posts` AS `Post` WHERE `Post`.`url` = 'setting-meta-tag-descriptions-and-keywords-under-cakephp-1-2' LIMIT 1110
8SELECT `Comment`.`id`, `Comment`.`post_id`, `Comment`.`body`, `Comment`.`author`, `Comment`.`url`, `Comment`.`email`, `Comment`.`ip`, `Comment`.`status`, `Comment`.`junk_score`, `Comment`.`created`, `Comment`.`modified` FROM `comments` AS `Comment` WHERE `Comment`.`status` = 2 AND `Comment`.`post_id` = (22) 221
9SELECT `Tag`.`id`, `Tag`.`tag`, `PostsTag`.`post_id`, `PostsTag`.`tag_id` FROM `tags` AS `Tag` JOIN `posts_tags` AS `PostsTag` ON (`PostsTag`.`post_id` = 22 AND `PostsTag`.`tag_id` = `Tag`.`id`) 110
10SELECT `Category`.`id`, `Category`.`category`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id` FROM `categories` AS `Category` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`post_id` = 22 AND `CategoriesPost`.`category_id` = `Category`.`id`) 110
11UPDATE `posts` AS `Post` SET `Post`.`hitcount` = Post.hitcount + 1 WHERE `Post`.`id` = 2210