vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Injecting code into existing template code (https://vborg.vbsupport.ru/showthread.php?t=284724)

NathanLedet 06-21-2012 01:19 AM

Injecting code into existing template code
 
I'm not sure how to do this. I'm not sure if I need to use hooks or if I can do a str_replace (is that even good practice with vB4?).

Basically, I want to write a plugin that injects an image in the header, after the "Welcome, user" text.

The easy thing would be to just modify the template, but I want to avoid modifying any of the core templates.

header template
PHP Code:

<li class="welcomelink">
     {
vb:rawphrase welcome_x_link_y
     {
vb:raw bbuserinfo.username}, 
     {
vb:link member, {vb:raw bbuserinfo}}}
//injected code here?
</li


kh99 06-21-2012 01:43 AM

The only way to do it without modifying the template (or doing something a lot more complicated) would be to do a str_replace at some point, either before or after it's rendered. But it can be difficult to figure out exactly what string to match.

Looking at the compiled header template from the database (which is what will be in the template cache), the part you want looks like this:

Code:

vB_Template_Runtime::linkBuild("member", $bbuserinfo) . '') . '</li>

and you'd want to insert your code just before the </li> at the end. So maybe something like this (maybe at hook parse_templates):

PHP Code:

$injected_code "something";
$find 'vB_Template_Runtime::linkBuild("member", $bbuserinfo) . \'\') . \'</li>';
$replace 'vB_Template_Runtime::linkBuild("member", $bbuserinfo) . \'\') . \'' $injected_code '</li>';

$vbulletin->templatecache['header'] = str_replace($find$replace$vbulletin->templatecache['header']); 

(BTW, I haven't tried this at all)


You may also be interested in this mod: www.vbulletin.org/forum/showthread.php?t=152931 although I haven't tried it myself.

NathanLedet 06-21-2012 07:17 AM

Very helpful. Thank you.

And yes, that mod will most definitely help out. Thanks for the link!


All times are GMT. The time now is 08:43 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.01825 seconds
  • Memory Usage 1,722KB
  • 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
  • (1)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)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