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.