Adding metatag only to homepage

I’m building a module to add some metatag for seo purpose, like google webmaster tools verification metatag.

I need to put this metatag only on homepage. I have created the module with an interceptor that add this on cbui_beforeHeadEnd. However this will show the meta on all pages of the site.

Is there a method to put this content only on homepage?

My method may not be the best method for this, but I’ve been using a custom layout for my homepage screen vs. the page.

/layouts/home.cfm

/layouts/page.cfm

Then on my main page in the Administrator, I’ve linked that page to my home layout vs. page layout for other pages. This allows me to do whatever customizations directly in the template instead of the handler for the homepage.

Again, it’s my way, maybe not the best way.

Marco G. Williams

Information Technology Manager

Global Electronic Technology

949.380.0345 x209

A few things you might explore.

If you keep the interceptor you’re already using, you could use CBHelper and check isIndexView() to determine if you’re on the blog home page or not.

Another option would be to leverage the cbui_onIndex interception point, which gets announced only for the blog home page.

Thanks!

Marco,

It sounds like you have hard coded your changes into the system, this is certainly not the best approach at all.

Too add any meta tag information, you need to look into using interceptors and how to create a module, I have many blog articles on this that will help get you started as well as a list of connect session on the ContentBox website as well as ColdBox as it is written using ColdBox.

If you are hacking like this, you are only opening yourself up to further heartache and frustration when things don’t work and low and behold you will also find getting help harder because you haven’t bothered to take the time to do the job correctly. If you need help and a pointer then we can help there, but not when we get a question like this.

Or you could look into the option in the Admin Dashboard where you can add information to the header etc, and add it there, this will become global to any layout theme your using and I would advise not to go this route if you don’t want it in all themes.

Thanks Andrew,

I’m familiar with interceptors and how to create modules, but I threw my solution out there (however bad the solution was) because I’m learning about themes / layouts and I recently had an occasion in which I really needed lots’ of customization in the view for the homepage vs. any other page in the site. So there is where my response to the actual question came from. You are right however, it is probably not the correct or best approach for his specific need.

The original question came from Francesco Pepe, whom still is looking for an answer. While I don’t know the best practice or technique here, it wouldn’t hurt to post a link or two to one of those blog posts that would steer this guy in the right direction.

Thanks,

Marco G. Williams

Information Technology Manager

Global Electronic Technology

949.380.0345 x209

Sure…

http://www.andyscott.id.au/blog/writing-a-coldbox-module-to-add-content-after-the-end-of-a-post

This shows how to hook into one of the Entry Interception points, it could easily be modified to hook into any of the header or body Interception Points.

However there is one thing one needs to take into consideration when doing anything with Google+ Authorship. ContentBox sends an incorrect header response for anything that is cached. Google+ doesn’t like this and one will have to modify ContentBox for Google+

Which the the problem and solution is found here.

http://www.andyscott.id.au/blog/known-contentbox-issue-and-posting-to-google

Also there is a ticket that has been raised, and Luis is yet to fix this issue. The ticket can be found here.

https://ortussolutions.atlassian.net/browse/CONTENTBOX-378?jql=project%20%3D%20CONTENTBOX%20AND%20reporter%20%3D%20currentUser()

Which looks like it will be released as part of ContentBox V2.0, so in the meantime one is going to have to manually patch the code to the blog post I provided above.

Thanks Andrew,

I’m familiar with interceptors and how to create modules, but I threw my solution out there (however bad the solution was) because I’m learning about themes / layouts and I recently had an occasion in which I really needed lots’ of customization in the view for the homepage vs. any other page in the site. So there is where my response to the actual question came from. You are right however, it is probably not the correct or best approach for his specific need.

The original question came from Francesco Pepe, whom still is looking for an answer. While I don’t know the best practice or technique here, it wouldn’t hurt to post a link or two to one of those blog posts that would steer this guy in the right direction.

Thanks,

Marco G. Williams

Information Technology Manager

Global Electronic Technology

949.380.0345 x209

Well said Brad, I didn’t mean to come across as it is not a good idea. I just think Hard coding things like this is just not the right way for obvious reasons.

I know it is hard to grasp the concept of what and how themes (layouts, I hate the term layout in ContentBox as it is a theme anyway) actually work and knowing what and where and how to do something is not easy to find at the moment.

Yeah, but thankfully the code in the default layout is pretty well commented and it’s semi-easy to follow :smiley: yay for that!

Marco G. Williams

Information Technology Manager

Global Electronic Technology

949.380.0345 x209

So, the reply to the post is that is not possible to add a metatag only to homepage? I use interceptor, but it will be fired on every page. Not a great problem in my case, but seems this is a missing feature. However I will post code to my module as soon as it’s possible to show what I’mtrying to do.

No that is not the answer.

There are a number of things you can do, but the most easy is to do something like this if it is never likely to change.

public void function cbui_beforeHeadEnd() event=“please past event here” {
}

So if the home page is the default ContentBox, it will be contentbox-ui:blog.index, if in your case it is to a particular page then you would do something along the lines of contentbox-ui:page.slugname or something like that, you may need to search the code for the right event.

Now if the page is likely to change, then the same rule sort of applies, you would write something like this.

public void function cbui_beforeHeadEnd() event=“contentbox-ui” {
}

This will ensure that the interceptor is only run for the front end, or if you could add the page to make it.

public void function cbui_beforeHeadEnd() event=“contentbox-ui:page” {
}

Which ensures that the interceptor will only run when the page is called. Then you could do the slug check etc with in the body of the function. And if it is only the default home page, the event would something like mentioned above with blog.index.

Or you could then just change the event to use the UI, then check the URL inside the method.

Hope that helps.

Andrew I have tried with this:

`

public void function cbui_beforeHeadEnd(event, interceptData) event=“contentbox-ui:blog.index” {
}

`

However it seems get fired on every page. Am I doing something wrong?

Did you re-init after the change?

Yes of course, I reinit and cklear cache.

The correct annotation is “eventPattern”

http://wiki.coldbox.org/wiki/Interceptors.cfm#eventpattern_annotation

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Then do this in that interceptor.

public void function cbui_beforeHeadEnd() event=“contentbox-ui” {
writeDump(rc); abort;
}

This will tell you the event that will be needed to check for.

hehe, ooops…

Yeah!!! this works, many thanks!!!

You can also check fo the home page and adding you meta tag if this condition is true:

cfif prc.page.getSlug() EQ cb.getHomePage()>

Cheers

Joel,

First the cbhelper, is the other option I was trying to think of and not being near a computer with ContentBox installed had to go by memory. But the checks are cheaper with the eventPattern as it is not reliant on calling the DB for no reason. Just something to consider there. The best option is to actually check for the eventPattern so that no other code is run unnecessarily.

The interception point for cbui_onIndex is another point to consider, however the only drawback with this is that the data is not added to the header, which can be very important for SEO stuff.