vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 4 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=242)
-   -   [vBulletin 4] Simple way of including an external PHP file (https://vborg.vbsupport.ru/showthread.php?t=242454)

Crimm 05-12-2010 10:00 PM

[vBulletin 4] Simple way of including an external PHP file
 
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!

frostyx 05-27-2010 06:07 PM

This is excellent, it works great on the forum and blog pages but it won't load on the home page for me. Any advice?

Crimm 05-28-2010 09:18 AM

Helped someone else with the same problem. Globalstart isn't a hook on the home page.

I can't currently give you documentation, but if you use init_start it should work. For optimization reasons though I do not suggest that. I can offer more after the weekend is over if needed.

Thanks.

philwareham 07-06-2010 04:36 PM

Hi,
Thanks for the tips. How would I use this idea to replicate a php 'echo file_get_contents' instead of a 'require_once'?
Cheers,
Phil

Crimm 07-09-2010 05:00 PM

I'm not 100% sure. I haven't done that yet.

Stupid question, but have you tried swapping the two?

philwareham 07-12-2010 03:31 PM

Yep, this works thanks...
Code:

// Textpattern External Output: body-social
ob_start();
    echo file_get_contents('http://mydomain/?rah_external_output=body-social');
    $php_include = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('footer',array('txp_body_social' => $php_include));

Cool, this will make vBulletin really easy to integrate with my CMS templates. Great stuff.

ragtek 07-12-2010 05:45 PM

Quote:

Originally Posted by philwareham (Post 2068121)
Yep, this works thanks...
Code:

// Textpattern External Output: body-social
ob_start();
    echo file_get_contents('http://mydomain/?rah_external_output=body-social');
    $php_include = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('footer',array('txp_body_social' => $php_include));

Cool, this will make vBulletin really easy to integrate with my CMS templates. Great stuff.

You don't need the output buffer!
$php_include = file_get_contents('...'); would also work;)

Centrix 07-14-2010 01:30 PM

I tried this, but it made my forum crash miserably. I had to restore a database backup in order for it to work again.

Quote:

Originally Posted by Crimm (Post 2036621)
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));

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

I made a custom template and I dont know which hook location to use for this...? if I use global start my site and forum (site is linked to the forums) is just the contents of the php file im trying to include.

Triky 07-15-2010 01:47 PM

When I create the plugin and I activate it, I get a blank page in my forum. This is the code I'm using in the plugin:

PHP Code:

ob_start();
  require_once(
'../includes/kbar.php');
  
$kbar ob_get_contents();
ob_end_clean();
vB_Template::preRegister('header',array('php_include' => $php_include)); 

And this is what I'm using on the template header:

Code:

{vb:raw kbar}
Why? Can you please help me?

Triky 07-20-2010 11:48 AM

Can please somebody help me out?

philwareham 07-20-2010 11:55 AM

Looks OK to me, have you made the plugin hook 'global_start'?

Maybe try a full server path for kbar.php?

Triky 07-20-2010 11:21 PM

Yes, I'm using global_start. Using a full server path I can get the forum to work (previously I was getting a blank forum page).. but nothing is being included. And yes, I'm sure the file is where it is supposed to be.

This is strange.. please help me! :)

ragtek 07-21-2010 04:18 AM

And that's the problem;)

There is no global_start hook anymore;)

Use global_bootstrap_init_start

philwareham 07-21-2010 07:19 AM

Quote:

Originally Posted by ragtek (Post 2072286)
There is no global_start hook anymore;)

Huh? I'm using it fine on my v4.0.5 install for hooks in the header and footer templates. Confused.

ragtek 07-21-2010 07:47 AM

PHP Code:

// Deprecated as of release 4.0.2, replaced by global_bootstrap_init_start
($hook vBulletinHook::fetch_hook('global_start')) ? eval($hook) : false;

$bootstrap->load_style();

// legacy code needs this
$permissions $vbulletin->userinfo['permissions'];

// Deprecated as of release 4.0.2, replaced by global_bootstrap_complete
($hook vBulletinHook::fetch_hook('global_setup_complete')) ? eval($hook) : false


philwareham 07-21-2010 08:13 AM

Ah OK thanks, I'll change all occurrences of 'global_start' to 'global_bootstrap_init_start' to futureproof the hooks.

Triky 07-21-2010 12:16 PM

I was having problems both with the hook and the PHP code of the plugin. This is the correct code:

PHP Code:

ob_start();
  require_once(
'/path/to/public_html/includes/kbar.php');
  
$kbar ob_get_contents();
ob_end_clean();
vB_Template::preRegister('header',array('kbar' => $kbar)); 

Thank you!

southie 10-06-2010 06:49 PM

i am having the same issue, did you ever find a solution to this problem Centrix ? or anybody else who could help ?

Quote:

Originally Posted by Centrix (Post 2069043)
I tried this, but it made my forum crash miserably. I had to restore a database backup in order for it to work again.



I made a custom template and I dont know which hook location to use for this...? if I use global start my site and forum (site is linked to the forums) is just the contents of the php file im trying to include.


webmaster74 10-09-2010 12:54 AM

Quote:

Originally Posted by Crimm (Post 2036621)
vB_Template::preRegister('TEMPLATE YOU ARE USING',array('php_include' => $php_include));[/code]

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

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::preRegister('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 ??

hihello 11-19-2010 06:23 PM

The included page shows up but it's showing on every page. I used the parse_template hook. Do I need to include anything else to only show one a specific page?

webmaster74 12-10-2010 10:51 AM

Quote:

Originally Posted by webmaster74 (Post 2108292)
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::preRegister('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 ?

BirdOPrey5 12-22-2010 01:32 AM

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.

fluidswork 12-22-2010 01:15 PM

Nice idea i will give this a try ..........

aileron79 12-22-2010 02:24 PM

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

BirdOPrey5 12-22-2010 03:38 PM

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

aileron79 12-22-2010 04:33 PM

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

BirdOPrey5 12-22-2010 04:49 PM

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.

aileron79 12-22-2010 08:48 PM

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?

BirdOPrey5 12-22-2010 11:20 PM

Quote:

Originally Posted by aileron79 (Post 2137787)
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.)

robert garrett 12-30-2010 12:43 PM

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

robert garrett 01-02-2011 03:45 PM

o.k. I got that to work, now how to get scripts that call the same page to work?

ehsanix 01-14-2011 07:17 PM

woooooooooooooooooooooow
very nice
thanx
thanx man
gooood job :)
exelent

ehsanix 01-15-2011 10:58 PM

How can I php file in my Sidebar include?
help :(

MMODisneyForums 01-27-2011 01:47 AM

This is a nice tutorial you have, I have read many, but this one is very clear and simple. But from all the tutorials I've tried, I can never get this to work. I want to have a php file output into the postbit_legacy template. So I made a plugin following this tutorial, using this:

Hook Location: global_start
Title: Testing
Execution Order: 5
And the PHP Code:
PHP Code:

ob_start();
 require_once(
'http://**********/creds.php');
  
$php_include ob_get_contents();
ob_end_clean();
vB_Template::preRegister('postbit_legacy',array('php_include' => $php_include)) 

Then in the postbit_legacy template I added in {vb:raw php_include} where it should go. Nothing shows up, and I get an error at the top of the page:
PHP Code:

Parse errorsyntax errorunexpected $end in /home/****/public_html/forums/global.php(29) : eval()'d code on line 7 

I get that error on pages that have posts on it. On the forum homepage, nothing shows up except:

PHP Code:

Parse errorsyntax errorunexpected $end in /home/mmodis/public_html/forums/global.php(29) : eval()'d code on line 7

Fatal error: Call to undefined function print_portal_output() in /home/mmodis/public_html/index.php on line 46 

If anyone can help me with this thanks, I have had problems with using plugins to include php files in the templates for too long. It always worked perfectly fine for me in vB 3.4. Thankyou for your time.

Note: Where the "****" are, just for posting the code here, I put those in to censor where the files are, just to be safe. I really do have the correct paths in the files uploaded to the site.

risestar 01-29-2011 02:52 AM

Well global_start hook location is now obsolete as of 4.0.3+ and 4.1. You should probably use the global_bootstrap_init_start hook instead if you are using a recent version. Also using $php_include as your variable might be causing problems so rename it to something unique. Your plugin code should probably be more like this

Code:



ob_start();
  require_once('http://www.xxxxxxx.com/creds.php');
  $creds = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('postbit_legacy',array('creds' => $creds));


and of course the call in the postbit_legacy template {vb:raw creds}


The parse error in your code is due to your missing the ";" at the end of your statement


Quote:

Originally Posted by MMODisneyForums (Post 2155109)
This is a nice tutorial you have, I have read many, but this one is very clear and simple. But from all the tutorials I've tried, I can never get this to work. I want to have a php file output into the postbit_legacy template. So I made a plugin following this tutorial, using this:

Hook Location: global_start
Title: Testing
Execution Order: 5
And the PHP Code:
PHP Code:

ob_start();
 require_once(
'http://**********/creds.php');
  
$php_include ob_get_contents();
ob_end_clean();
vB_Template::preRegister('postbit_legacy',array('php_include' => $php_include)) 

Then in the postbit_legacy template I added in {vb:raw php_include} where it should go. Nothing shows up, and I get an error at the top of the page:
PHP Code:

Parse errorsyntax errorunexpected $end in /home/****/public_html/forums/global.php(29) : eval()'d code on line 7 

I get that error on pages that have posts on it. On the forum homepage, nothing shows up except:

PHP Code:

Parse errorsyntax errorunexpected $end in /home/mmodis/public_html/forums/global.php(29) : eval()'d code on line 7

Fatal error: Call to undefined function print_portal_output() in /home/mmodis/public_html/index.php on line 46 

If anyone can help me with this thanks, I have had problems with using plugins to include php files in the templates for too long. It always worked perfectly fine for me in vB 3.4. Thankyou for your time.

Note: Where the "****" are, just for posting the code here, I put those in to censor where the files are, just to be safe. I really do have the correct paths in the files uploaded to the site.


MMODisneyForums 01-31-2011 05:54 PM

Thanks for the help. I tried all of that, and now every page in the forum is completely blank when I turn the plugin on. If I turn it off the forums are back. Is there any reason for this?

Boofo 01-31-2011 06:46 PM

I still don't understand why you guys just don't use the require_once to include the file in the php hook right before the code you want to use it with. You are taking the long way around doing it this way. Unless you need the included file for every page, it makes no sense to put it in the global hook.

risestar 01-31-2011 07:36 PM

What version of vbulletin are you using?

If you type in the script location directly from your browser, does it work? If not, then its a problem with your script.

Also, your php.ini might be set to disallow http php include calls, if you so need to enable it, or use the path call instead

Code:

ob_start();
  require_once('/path/to/your/website/creds.php');
  $creds = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('postbit_legacy',array('creds' => $creds));

Otherwise, its a typo, if you are using this code, make sure you cut and paste it, have the path/url correct



Quote:

Originally Posted by MMODisneyForums (Post 2156997)
Thanks for the help. I tried all of that, and now every page in the forum is completely blank when I turn the plugin on. If I turn it off the forums are back. Is there any reason for this?


MMODisneyForums 01-31-2011 08:31 PM

I'm using version 4.1.0. I tried changing my creds.php file to just simply echo "Test", and I still just get a plain white page on the forums. If I open the php file in my browser it correctly says Test. Should I change the hook location? Is there a better location for postbit_legacy? And I have copy pasted that code exactly in, and replaced the URL. What could be going wrong here? Thanks

Edit: I have noticed something else that is interesting. If I remove the {vb:raw creds} from the postbit_legacy template, there is still a blank page. If the plugin is on, theres a blank page, even if its not being called from the template. Hope this helps.

risestar 02-02-2011 02:48 AM

The plugin is fairly straight forward, as long as you have a valid script that you are calling and your plugin code is valid, you should be good to go.

you might have a php.ini config issue going on.

Create a new file in your forum root, call it test.php

Insert this

<?php include ("/server/path/to/your/test.php"); ?>

<?php include ("http://www.yoursite.com/forum/test.php"); ?>

/insert this

Then open the test.php in your browser

If they BOTH work, you should have your echoed text inserted twice. If only ONE works, or you get a php error, you have a php.ini config issue to work out.


All times are GMT. The time now is 02:31 PM.

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.01679 seconds
  • Memory Usage 1,890KB
  • 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
  • (9)bbcode_code_printable
  • (12)bbcode_php_printable
  • (9)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (40)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete