Referers in b2++

I’ve integrated part of the Nathan’s referer code into b2++. It’s in the form of a prefunction as it creates a global referers tag you can put into your site templates.
Currently it creates a different tag if you’re viewing a single post or a whole blog/category. This allows the template designer to use the postreferers tag when listing referers to a single post, or use refererlinks to list the site referers list. Add this code to your index.tpl to enable it:

{if $refererlinks != “”}
{foreach from=$refererlinks item=link}
<li> <a href=”{$link.referingURL}”>{$link.baseDomain}</a> ({$link.visitTimes})

Flavour it to suit your own blog design. Substitute $postreferers for $refererlinks and put the same code into post.tpl to see single post refererers. The final b2++ 0.6 will have this in place, and all you’ll need to do is update your templates from the Template Chooser.

Because of the caching on this site many referers were lost, I changed the referer plugin into a Smarty insert plugin.
Insert functions are executed every time a page is loaded and because of the way they operate, I’ve had to hard-code the html it outputs for the time being.
Anyway, ignore the instructions above. If you want to record and display referers on your blog on this site, then add the following code to index.tpl (anywhere will do, but make sure there’s enough space for it!)

{insert name=”getreferer” mode=”index”}

Add the following to post.tpl if you want to show referers to individual posts too:

{insert name=”getreferer” mode=”post”}

There’s some problem with it as is though, sometimes the insert function doesn’t execute. I’ll have a look at why tomorrow.

Update 2
I still don’t know why insert functions didn’t work, that’s something I’ll have to bring up on the Smarty mailing list tomorrow. I reverted to recording every referer on each request, but the list of referers for any page is only updated when the page cache is updated.

Author: Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

1 thought on “Referers in b2++”

  1. Hey, why not just {referers} ? with paramaters e.g. {referers sep=”
    ” global=TRUE} That’s what I’ve done for bblog. Also, it connects to the database when it’s already connected (presumeably) creating 2 connections. I’m not using caching yet so I can’t help you with the caching problem.

Leave a Reply