PDA

View Full Version : how to set "hook location"?


radiofranky
07-27-2011, 10:14 PM
Hi,
in the template no direct php code can be pasted. Therefore, you need to create a "plugin" if you want to execute a php script. But my question is how to figure out which hook location should I use?

For example, I want to add "total posts: total users:" to my header, above the logo img.

How do I do that?


Do I create a plugin and then? What hook location should I select? What happens if I can't find one just sounded right? :)

Thanks for the helps.


ob_start();
global $vbulletin, $db, $vbphrase;
//Begin Forum Stats
// forum stats start
$numbersmembers = $db->query_first("SELECT COUNT(*) AS users,MAX(userid) AS max FROM " . TABLE_PREFIX . "user");
$numbermembers = number_format($numbersmembers['users']);
$counter = $db->query_first("SELECT COUNT(postid) AS posts, COUNT(threadid) AS threads FROM " . TABLE_PREFIX . "post");
$totalposts=number_format($counter['posts']);
$countthreads = $db->query_first("SELECT COUNT(*) AS threads FROM " . TABLE_PREFIX . "thread");
$totalthreads=number_format($countthreads['threads']);
// forum stats end

// total online start
$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
$headerguests=$db->query_first("SELECT COUNT(*) AS count FROM " . TABLE_PREFIX . "session WHERE userid=0 AND lastactivity>$datecut");
$headerusers=$db->query_first("SELECT COUNT(DISTINCT(userid)) AS count FROM " . TABLE_PREFIX . "session WHERE " . TABLE_PREFIX . "session.userid>0 AND " . TABLE_PREFIX . "session.lastactivity>$datecut");
$headerguests=$headerguests[count];
$headerusers=$headerusers[count];
$totalonline=$headerguests+$headerusers;
// total online end

// get newest member name and userid start
$getnewestmember=$db->query_first("SELECT userid, username FROM " . TABLE_PREFIX . "user WHERE userid=$numbersmembers[max]");
$newusername = $getnewestmember['username'];
$newuserid = $getnewestmember['userid'];
// get newest member name and userid end

//End Forum Status
//SideBar - Begin forum Stats
$sb_stats='
<table width="100%" align="center"> <tr> <td class="" align="left"> <div class="smallfont"> <strong>Number of Members: </strong> '.$numbermembers.'<br /> <strong>Total Threads: </strong>'. $totalthreads.'<br /> <strong>Total Posts: </strong>'. $totalposts.'<br /> <strong>Currently Online: </strong>'. $totalonline.'<br /> <br /> <strong>Newest Member:</strong> <a href="'.$vboptions[bburl].'/member.php?u='.$newuserid.'"><b>'.$newusername.'</b></a> </td> </tr> </table>';
//SideBar = End forum Status
echo $sb_stats;
$output=ob_get_contents();
ob_end_clean();

kh99
07-27-2011, 11:24 PM
That's a good question, and I'm kind of curious to see if anyone else has an answer to this, because I really have a hard time imagining how it could be done without looking at the vb PHP code. In your case you want to add something to the header, so it has to be done before the header template is rendered. I happen to remember that a good hook location for that is parse_templates. I suppose if you didn't know that, you might search and find that the header template is rendered in the file includes/class_bootstrap.php around line 486, so you could then look at the code before that and notice at around line 414:

($hook = vBulletinHook::fetch_hook('parse_templates')) ? eval($hook) : false;

which is the line that evaluates any plugin code using hook "parse_templates".

So anyway, if you're doing a lot of modifications I would recommend finding an editor that lets you search across a set of php files (I think notepad++ does that, and it's free), then have the vb source on your local computer so you can easily look at it when you need to figure something out.

Lynne
07-28-2011, 02:10 AM
I always look at the list of plugins called when in debug mode (they are all shown in the bottom right corner). And, depending on if this is a page specific change or not, I would look at the php page itself and see what hooks are there.

radiofranky
07-28-2011, 06:22 AM
that's too much for me.. :) i guess i need to play vb a little more before I can grasp the idea

is there an easier way to display forum total post and total user number in the header section?
cheers

--------------- Added 1311838146 at 1311838146 ---------------

That's a good question, and I'm kind of curious to see if anyone else has an answer to this, because I really have a hard time imagining how it could be done without looking at the vb PHP code. In your case you want to add something to the header, so it has to be done before the header template is rendered. I happen to remember that a good hook location for that is parse_templates. I suppose if you didn't know that, you might search and find that the header template is rendered in the file includes/class_bootstrap.php around line 486, so you could then look at the code before that and notice at around line 414:

($hook = vBulletinHook::fetch_hook('parse_templates')) ? eval($hook) : false;

which is the line that evaluates any plugin code using hook "parse_templates".

So anyway, if you're doing a lot of modifications I would recommend finding an editor that lets you search across a set of php files (I think notepad++ does that, and it's free), then have the vb source on your local computer so you can easily look at it when you need to figure something out.


thanks. But how did you figure out that this is where plugin code gets evaluated? Is there any documentation from vb or just by doing try and error?

what does {vb raw totalpost} menas? and how to use it in the templates?
If I see it in xyz template, can I use it in zzz template? I tried it, but with no luck so far. It seems is returning null.

Badshah93
07-28-2011, 06:57 AM
that's too much for me.. :) i guess i need to play vb a little more before I can grasp the idea

is there an easier way to display forum total post and total user number in the header section?
cheers

--------------- Added 1311838146 at 1311838146 ---------------




thanks. But how did you figure out that this is where plugin code gets evaluated? Is there any documentation from vb or just by doing try and error?

what does {vb raw totalpost} menas? and how to use it in the templates?
If I see it in xyz template, can I use it in zzz template? I tried it, but with no luck so far. It seems is returning null.

You look to confused but that happens if it is ur first time.

{vb:raw totalpost} = $totalpost


First will start from VB3


For Ex:


Suppose i created a plugin:

Hook: forumhome_complete

Code:

$myvar = 2;

//and other things....


As you can see in plugin we assigned $myvar = 2,
so we can use $myvar variable in FORUMHOME template.

Now some question's comes in our mind:

1. can i use $myvar in other place like user profile.

Ans. No, because forumhome_complete plugin hook execute at homepage only. So you need to find a diff hook for it, you can use debug mode to get the right hook.



Now lets come to VB4 :)

All the things will be same as in vb3, except few changes.


Hook: forumhome_complete

Code:

$myvar = 2;

//and other things....


vB_Template::preRegister('FORUMHOME',array('myvar' => $myvar)); //This line register myvar in FORUMHOME template.


Now in FORUMHOME template u can use

{vb:raw myvar}




Read This article to know more about Registering variable's in templates.

https://vborg.vbsupport.ru/showthread.php?t=228078

radiofranky
07-28-2011, 08:26 PM
Thanks for the example.

Let's say I find {vb raw: totalpost} in FORUMHOME and I would like to use the same {vb raw: totalpost} in HEADER.

totalpost is a VB build-in variable. Is there a way to find out all the VB build-in variables and it's PHP location?

Where do I find which PHP file should I register with? and do I just add this line below FORUMHOME?


vB_Template::preRegister('FORUMHOME',array('totalp ost' => $totalpost));
vB_Template::preRegister('HEADER',array('totalpost ' => $totalpost)); //This line register myvar in FORUMHOME template.

kh99
07-28-2011, 09:00 PM
{vb raw: totalposts} is used in the template FORUMHOME, and that template is rendered at the end of forum.php. So we look in forum.php and see that there's code that calculates $totalposts, and later a line that registers $totalpost in that template. Now you want to use it in header, well, there's a problem. Header is included in every page, not just forum.php, but the code to calculate $totalposts is only in forum.php. So to make it work in the header you'd need to find the code in forum.php that does the calculations, copy it somewhere before the header template is rendered (like a plugin using hook parse_templates), then register the variable in the header template (and of course, insert it in the header template).

Now there's one problem with that - the code calculates the value for a specific forum, and not every page has just one specific forum associated with it. So what would you do in those cases? And what are you trying to do, do you want the value to display on all pages or do you just want to move it to a different location on the forum page?

radiofranky
07-28-2011, 09:36 PM
All I want is to display "total posts:" and "total users", just like in slickdeals.net which I believe they uses VB.

thanks

Lynne
07-29-2011, 03:31 PM
Isn't there a modification that does this already? (I thought I had seen others ask about doing this and then being pointed to a modification.)