cakephp logo

SEO meta tags under Cakephp 1.2

Setting your pages meta tag keywords and description is simple under CakePHP 1.2, all one needs is the Html Helper's meta() function. Probably the easiest way is to see how its done is with an example, this code is added to your view files :-

Show Plain Text
PHP code
  1. echo $html->meta('keywords', 'your keywords goes here', array('type' => 'keywords'), false);
  2. echo $html->meta('description', 'your description goes here', array('type' => 'description'), false);

In my original article on meta tags I ended up setting a variable in the view and checking for the variables in the default layout file. I had tried the $html->meta() function originally but couldn't get it to work and didn't have time to figure things out.

It wasn't till I was trying to get some javascript code to output into the head section from a view using the $this->addScript() function that I realized I didn't have the echo $scripts_for_layout line in the default.ctp layout.

Not sure when or how I deleted it but in the words of Homer Simpson all I can say is DOH.

Update: 2 Dec 2008, I see an example of meta() function is now documented in the CakePHP book, please refer to this page Inserting-Well-Formatted-elements

Filed under: Cakephp  Tags: Seo

16 Responses to “SEO meta tags under Cakephp 1.2”

Thanks fellow developer, I'm on a time crunch and this was a little piece of treasure in the form of a blog post that came up on google. You rock!

So would I take it that we would write this into the View?

@Chris C2 Yes that is correct Chris you can place the code in your view or set a variable in the view which is passed to the layout which runs the meta code

Thanks, was very useful...

Thanks for all the tips. but till now i have....

Thanks.

note that in 1.3 you would do echo $html->meta('keywords', 'your keywords goes here', array('type' => 'keywords', 'inline' => false));

simple and great post. exactly what I was looking for and since i found you in my first jfgi that means it works ;)

Thx

good post. Thx.

oops.. It didn't work for me. I am using cakephp 1.3.3 Fortunately i got the solution for the problem. In your view or controller file add :-

$this->set('description_for_layout', 'Enter your description here');
Then, in the header part of default.ctp, put this code..
<meta name='description' content='<?php echo $description_for_layout;?>' />
Thats it :) Enjoy Coding.

continued...

<meta name='description' content='<?php echo $description_for_layout; ?>' />
Enjoy Coding :)

hm!. The tags are getting truncated from the comments.

Thanks a lot, with karthik's update for the 1.3 version, it's exactly what I was looking for!

Same here. Was having problems with this because of updated syntax in 1.3. use array("inline" => false) as 3rd arg instead of a fourth arg as false.

Thanks for this little snippet - have added your blog to my personal list.

Sorry, comments have been closed for this post.
(default) 11 queries took 7 ms
NrQueryErrorAffectedNum. rowsTook (ms)
1DESCRIBE `posts`17171
2DESCRIBE `comments`11111
3DESCRIBE `tags`221
4DESCRIBE `categories`220
5DESCRIBE `posts_tags`221
6DESCRIBE `categories_posts`221
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` = 'seo-meta-tags-under-cakephp-1-2' LIMIT 1111
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` = (60) 16161
9SELECT `Tag`.`id`, `Tag`.`tag`, `PostsTag`.`post_id`, `PostsTag`.`tag_id` FROM `tags` AS `Tag` JOIN `posts_tags` AS `PostsTag` ON (`PostsTag`.`post_id` = 60 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` = 60 AND `CategoriesPost`.`category_id` = `Category`.`id`) 110
11UPDATE `posts` AS `Post` SET `Post`.`hitcount` = Post.hitcount + 1 WHERE `Post`.`id` = 6010