PDA

View Full Version : REGEX in vB Template Conditional


imported_silkroad
10-30-2009, 06:32 PM
Hello!

We have some code we want to omit from the template if the USER_AGENT is GoogleBot, Mediapartners-Google, etc. For example (not working code)

<if condition="!in_array($_SERVER['HTTP_USER_AGENT'], array('GoogleBot','Mediapartners-Google'))">
<!-- this is the code -->
</if>


The user agent strings are:


Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Mediapartners-Google

So, the template condition above works well for Mediaparters-Google but does not match the longer GoogleBot string.

Is there a way to simply add some type of REGEX to the conditional, so 'GoogleBot' will match the longer user agent string?

Thanks!!

RenatoMN
10-31-2009, 02:28 AM
You will need to create a Plugin.

Product: vBulletin
Name: Hide Content From Bots
Hook: showthread_start
Execution order: 5
// bots list ([] = add a new item in the end of an array)
$hide_content_agent_list[] = "GoogleBot";
$hide_content_agent_list[] = "Mediapartners-Google";
$hide_content_agent_list[] = "ad more...";

// get the user agent
$hide_content_useragent = $_SERVER['HTTP_USER_AGENT'];

// the default is true, false for bots (<else />)
$show_for_humans = true;

foreach($hide_content_agent_list as $hcag) {
if (preg_match("/$hcag/i", "$hide_content_useragent")) {
$show_for_humans = false;
break;
}
}
Plugin is active: Yes //do not forget it :)

Then use the condition either this:

<if condition="$show_for_humans">
code to be show only for humans
</if>

<if condition="$show_for_humans">
code to be show only for humans
<else />
the replacement text for bots
</if>

imported_silkroad
10-31-2009, 06:05 AM
Thanks! I'll give it a "plug" and see how it works!

--------------- Added 1256975400 at 1256975400 ---------------

I could not get this to work (for our purposes) in the hook location showthread_start, but I was able to get it to work in postbit_display_start.

Perhaps this is because the content we are masking is in the content of the postbit template?

RenatoMN
10-31-2009, 01:31 PM
Yes. Sorry for not asking or better explaining this.

Leave as postbit_display_start if you're not going to use it elsewhere, or try another location, like global_start if you're going to use it widely in your boards.

imported_silkroad
10-31-2009, 03:33 PM
Yes. Sorry for not asking or better explaining this.

Leave as postbit_display_start if you're not going to use it elsewhere, or try another location, like global_start if you're going to use it widely in your boards.

Hi RenatoMN!

Oh! No need to apologize. Your code worked fine. Here is final version I used:

// bots list ([] = add a new item in the end of an array)
$hide_content_agent_list[] = "GoogleBot";
$hide_content_agent_list[] = "Mediapartners-Google";

// get the user agent
$hide_content_useragent = $_SERVER['HTTP_USER_AGENT'];

// the default is true, false for bots (<else />)
$show_for_humans = TRUE;

foreach($hide_content_agent_list as $hcag) {
if (preg_match("/$hcag/i", "$hide_content_useragent")) {
$show_for_humans = FALSE;
break;
}
}

I may add some more bots later. Thanks again! This is very useful for everyone.

RenatoMN
10-31-2009, 05:37 PM
I'm glad it worked (I didn't even tested :p )

If you (or anybody) want this as a product, with ACP options (a bot list management), send me a PM so we can discuss details and price.

Regards,

imported_silkroad
11-01-2009, 05:43 AM
FWIW, I cannot get this plugin to work in postbit if I move it to the hook global_start however it works fine if the hook is postbit_display_start.