Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[vBulletin 4] Simple way of including an external PHP file
Crimm's Avatar
Crimm
Join Date: Feb 2007
Posts: 170

 

Show Printable Version Email this Page Subscription
Crimm Crimm is offline 05-12-2010, 10:00 PM

There are other articles out there on variables, templates, etc on vBulletin 4. This is a simple example of including an external PHP files like you used to be able to do here:

http://www.vbulletin.com/forum/showt...P-or-HTML-File

Thanks to this Blog post by David IB http://www.vbulletin.com/forum/entry...s-to-templates and this article by cellarius https://vborg.vbsupport.ru/showthread.php?t=228078

I have figured out it's only a simple extra step.

Step 1: Create a new plugin
  • Hook Location: What area of the forums you want this variable to appear. Don't know where? Use global_start
  • Title: Give it a title
  • Execution order: Your choice
  • Plugin PHP Code:

    Code:
    ob_start();
      require_once('LOCATION OF EXTERNAL FILE');
      $php_include = ob_get_contents();
    ob_end_clean();
    vB_Template::preRegister('TEMPLATE YOU ARE USING',array('php_include' => $php_include));

Step 2: You will have to figure out these two entries for yourself: LOCATION OF EXTERNAL FILE & Hook Location

To give you an example of what you should use is that if you want to display your external PHP file on your Forum's Home. Then replace these two with these values:

Hook Location with forumhome_start
TEMPLATE YOU ARE USING with FORUMHOME

Keep in mind that global_start will still be acceptable, but it's extra loading time where it's not needed. Therefore choosing the optimum hook location is better for your performance overall.

Step 3: Visit the Style Manager -> TEMPLATE YOU ARE USING and place the variable in your style where you want it. You will have to use the new format.

Code:
{vb:raw php_include}
That's it - Pretty simple; see?

Notes, If you want to:

Include this PHP file in multiple templates then preRegister it for the multiple templates:

Code:
vB_Template::preRegister('TEMPLATE YOU ARE USING',array('php_include' => $php_include));
vB_Template::preRegister('TEMPLATE YOU ARE USING 2',array('php_include' => $php_include));
Thanks to David IB again.

I'm still learning as I go with vb4, but if I learn some more notes to add... I'll drop by here.

I hope that helps some one out there!
Reply With Quote
  #22  
Old 12-10-2010, 10:51 AM
webmaster74 webmaster74 is offline
 
Join Date: Mar 2007
Posts: 193
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by webmaster74 View Post
I created this plug-in and activated it.

ob_start();
include('/home/mydomain/public_html/php/includes/ineverypost.inc.php');
$includedineverypost = ob_get_contents();
ob_end_clean();


vB_Template:reRegister('postbit_display_complete ',array('includedineverypost' => $includedineverypost));

in vb 3.8.6, this was hooked to 'postbit_display_complete'

Now with VB4, there is no template for post_display_complete....

I tried "postbit" it did notwork. I tried other postbit related templates, this still did not work.

any help ??

any generous person wants to share his knowledge ?

--------------- Added 11 Dec 2010 at 03:23 ---------------

would any kind person help with this ?
Reply With Quote
  #23  
Old 12-22-2010, 01:32 AM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

postbit_display_complete is a php code hook... not a template... the template is named postbit and it's the same in VB3 and VB4.
Reply With Quote
  #24  
Old 12-22-2010, 01:15 PM
fluidswork's Avatar
fluidswork fluidswork is offline
 
Join Date: Apr 2010
Location: India
Posts: 143
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nice idea i will give this a try ..........
Reply With Quote
  #25  
Old 12-22-2010, 02:24 PM
aileron79 aileron79 is offline
 
Join Date: Apr 2009
Posts: 36
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi everybody,
sorry in advance for me maybe talking rubbish, but I am completely new to developing plugin code for vbulletin. I am quite experienced in coding PHP, though. Boofo pointed me in the right direction (thanks for that), but, however, I am still facing the following problem:

I want to extend the list of online users (online.php) by another column. Information within that column is not extracted from the database but fetched from a third party. This has to be done for each user that shows up in the list of online users - but I have no idea which hook I may use.

At the moment, my plugin works pretty fine - but it is a bad mixture of source code hacking and template modifications. If any future upgrade replaces online.php, all my changes are gone. I added a very few lines of code in online.php (v4.1.0 PL2) starting from line 414, which basically adds another value to the $userinfo array which (as if this variable is registered) can be accessed from the whoisonlinebit template, where the output is generated from the additional value.

As mentioned before - so far, everything works fine but I truly hate source code modifications as they are gone after an update. Therefore, it would be great to know if there is another way of executing code (maybe in an external file) for each line in the result set. If I interpret the PHP source in online.php correctly, anything that should be executed for each user name should go inside the while loop on line 393. The instructions above explain how to use external files with hooks but I just can't figure out which hook to use in that case...

Is there a way to execute PHP code from templates without having a hook?

As I said, I am completely new to this stuff, please help...
Reply With Quote
  #26  
Old 12-22-2010, 03:38 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

There's a hook at the very end of the while loop:
PHP Code:
        ($hook vBulletinHook::fetch_hook('online_user')) ? eval($hook) : false
So if you can use that hook, "online_user."
Reply With Quote
  #27  
Old 12-22-2010, 04:33 PM
aileron79 aileron79 is offline
 
Join Date: Apr 2009
Posts: 36
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Indeed, I have found that hook and I thought it would be what I was looking for, but obviously this hook only affects spiders/guests, according to the comment on line 449:

PHP Code:
while ($users $db->fetch_array($allusers))
{
    if (
$users['userid'])
    { 
// Reg'd Member
...
    }
    else
    { 
// Guest or Spider..
        
$spider '';
...

        
$guests["$count"]['count'] = $count 1;
        
$guests["$count"]['useragent'] = htmlspecialchars_uni($users['useragent']);
        
$count++;

        (
$hook vBulletinHook::fetch_hook('online_user')) ? eval($hook) : false;
    }

The hook is inside the else branch which obviously handles unregistered users, the code I have added is in the if branch... Did I misinterpret anything...?
Reply With Quote
  #28  
Old 12-22-2010, 04:49 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Probably not... although it's of no use when releasing a mod to others you can add your own hooks to the code anywhere you want- so if it's just for you adding a 1 line hook and keeping the bulk of the code in a plugin will make life easier than keeping all edits in the files.
Reply With Quote
  #29  
Old 12-22-2010, 08:48 PM
aileron79 aileron79 is offline
 
Join Date: Apr 2009
Posts: 36
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for replying to fast! Well, that of course would be possible and still, this is probably the best idea. I still could add instructions on how to create the custom hook in my install guide. However, I consider this hook a bug... I just can't believe it makes any sense in that position. But as mentioned before, I am quite new to this plugin system, probably I just do not understand enough...

But am I right that there is no way to achieve what I want without modifying the original vb source code?
Reply With Quote
  #30  
Old 12-22-2010, 11:20 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by aileron79 View Post
But am I right that there is no way to achieve what I want without modifying the original vb source code?
Best I can tell if you really need that location you would have to manually edit the source code but I'm far from an 'expert' on this topic myself.

However if you're fetching data from an external source could you not just "run the loop" a second time and collect the data needed per user id, and put the data in an array who's keys are the userid's themselves so it's say $mydata[1] for userid 1, $mydata[200] for user 200, etc... then call it in the template where needed (after pre-registering it of course.)
Reply With Quote
  #31  
Old 12-30-2010, 12:43 PM
robert garrett robert garrett is offline
 
Join Date: Mar 2010
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
ob_start();
  require_once(
'/home/echoca/public_html/news/journals1.php');
  
$php_journal ob_get_contents();
ob_end_clean();
vB_Template::preRegister('echo_journals',array('php_journal' => $php_journal)); 
tried also leaving custom_ out this is not working at all.

global bootstrap init start, is the hook I am using. I had some bugs in the code when I first included it, and it showed up as I worked the bugs out, and the code ran clean, it made it through the hook. Thats great. I clicked on the tab, that is linked to the template I added it to, and it just wont display Ideas anybody?

RG
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:21 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04705 seconds
  • Memory Usage 2,345KB
  • Queries Executed 26 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (3)bbcode_code
  • (3)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (11)post_thanks_box
  • (7)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete