vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   REGEX in vB Template Conditional (https://vborg.vbsupport.ru/showthread.php?t=227107)

imported_silkroad 10-30-2009 06:32 PM

REGEX in vB Template Conditional
 
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)

Code:

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

The user agent strings are:
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
PHP Code:

// 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:

Code:

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

Code:

<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 [DATE]1256975400[/DATE] at [TIME]1256975400[/TIME] ---------------

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

Quote:

Originally Posted by RenatoMN (Post 1908679)
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:

PHP Code:

// 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.


All times are GMT. The time now is 08:07 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01003 seconds
  • Memory Usage 1,740KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete