The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Writing my first plug-in/product, several newbie questions.
Disclaimer: I've been reading the API and will continue to do so, just trying to get a feel for this and hopefully some folks will be patient enough to do a little hand-holding.
I'm trying to write my first plugin, something extremely basic, just to get the feel for how it works. I'm decent with template edits and variables, but I've never attempted writing a plugin before, and would really like to. That said, I'm looking to write a simple plugin to drop a variable into a template. In FORUMHOME, here's the default Who's Online: Code:
<a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>) Code:
Invisible: $numberinvisible Code:
<a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>) Invisible: $numberinvisible Code:
($hook = vBulletinHook::fetch_hook('forumhome_loggedinuser')) ? eval($hook) : false; So in using the basic code from the howto post, I come up with something like: Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <plugins> <plugin active="1"> <title>Chris's Newbie Plugin</title> <hookname>forumhome_loggedinuser</hookname> <phpcode> <![CDATA[ ]]> </phpcode> </plugin> </plugins> A: Where the actual code goes. B: How I tell vB where to actually put it, (Eg: "directly following $vbphrase[x_members_and_y_guests]</phrase>) ". I'm guessing that the code that I want to inject goes inside the CDATA brackets, like this: Code:
<![CDATA[Invisible: $numberinvisible]]> If anyone could shed some light on where I should go from here, I'd really appreciate the help. In the meantime I'll keep scouring the docs. :bunny: Update #1 Here's what I have so far, but I don't think I'm going about it correctly: Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <product productid="chris777_showinvisible" active="1"> <title>Show Invisible Users</title> <description>Will add a count of invisible users in WGO.</description> <version>1.0.0</version> <codes> </codes> <templates> </templates> <plugins> <plugin active="1"> <title>Show Invisible Users</title> <hookname>forumhome_loggedinuser</hookname> <phpcode><![CDATA[ if ($totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)) { Invisible: $numberinvisible; } ]]> </phpcode> </plugin> </plugins> <phrases> </phrases> <options> </options> </product> [high]* Guest210212002 scratches his head and continues to dig.[/high] |
#2
|
|||
|
|||
There is no need to create a plugin to show the number of invisible users online.
Edit the FORUMHOME template and add Invisible: $numberinvisible anywhere you like. The only weird thing about this is that I made the main admin invisible but it stayed at zero. I logged in with my other username which is in the "Registered Users" group and set him to invisible and it worked. Edit: Code:
<phpcode><![CDATA[ if ($totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)) { Invisible: $numberinvisible; } ]]> </phpcode> So change this Invisible: $numberinvisible; to $myvar = Invisible: . " $numberinvisible; In the appropriate template just add the variable $myvar in it and it'll output the text assigned to it. |
#3
|
|||
|
|||
Thanks very much harmor. I realize that this is super basic and it'd be far quicker to just do the template edit - I'm just using this as a stepping stone to get a feel for the actual process. 99% of the "modding" that I do are template edits, and I'd love to be able to release some of them as products. I think if I can get this working and learn the basics, I can build off of that to make more complex/useful bits.
Where would I actually define that variable in the plugin? Here's what I have right now, just adding in a phrase instead of the text. I'll start looking for the variable stuff from here. Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <product productid="chris777_showinvisible" active="1"> <title>Show Invisible Users</title> <description>Will add a count of invisible users in WGO.</description> <version>1.0.0</version> <codes> </codes> <templates> </templates> <plugins> <plugin active="1"> <title>Show Invisible Users</title> <hookname>forumhome_loggedinuser</hookname> <phpcode><![CDATA[ if ($totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)) { $vbphrase[online_invisible] $numberinvisible; } ]]> </phpcode> </plugin> </plugins> <phrases> <phrasetype name="GLOBAL" fieldname="global"> <phrase name="online_invisible"> <![CDATA[ Invisible: ]]> </phrase> </phrasetype> </phrases> <options> </options> </product> Code:
if ($totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)) |
#4
|
|||
|
|||
Well first let's start off with a simple plugin so you know the basics.
Let's create a plugin that'll display an easter egg if you type in "index.php?chris777" Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <product productid="chris777_easteregg" active="1"> <title>Easter Egg</title> <description>Will show an easter egg</description> <version>1.0.0</version> <codes> </codes> <templates> </templates> <plugins> <plugin active="1"> <title>Easter Egg</title> <hookname>forumhome_complete</hookname> <phpcode><![CDATA[ if($_REQUEST['do'] == "chris777") { $easter_egg = "Easter Egg Here"; } ]]> </phpcode> </plugin> </plugins> <phrases> </phrases> <options> </options> </product> Above $header Add $easter_egg<br /><br /> Let's take a closer look of why it works. PHP Code:
The reason why you assign the text to a variable is so you can place it into a template, otherwise the text doesn't know where to go, I suppose. I'm sorry but I'm not good at explaining how things work. |
#5
|
|||
|
|||
Thanks again, that definitely helps. I've been pining over the API all day, and either I'm senile, or can you not do direct template edits with plugins unless you're throwing a whole new section under the hook location?
For example, Paul M's Members Who Have Visited Today hack edits the forumhome template, and (albeit a LOT more complicated than what I'm trying to do) adds a section. Is it not possible to have a plugin/product that directly edits templates in certain spots? I realize that what I'm doing is pretty pointless and is much easier to just edit in the template, but like I said I'm trying to use it as a stepping stone to bigger/more complex stuff. I've done a LOT of template work on my site, and I'd love to be able to share some of it as a simple product install. Thanks very much once again, I really appreciate any advice/tips I can get. [high]* Guest210212002 cheers[/high] |
#6
|
||||
|
||||
I use this code in one of my hacks to add template code using a plugin:
Code:
$search_text = '$navbar'; $vbulletin->templatecache['FORUMHOME'] = str_replace($search_text, $search_text.fetch_template('temp_with_code_you_want_added'),$vbulletin->templatecache['FORUMHOME']); Basically, just replace the $navbar with a string from a template, in my case I'm using forumhome, if I'm understanding what you're trying to do, then I think you'd want to use the same. Try replacing $navbar with "$vbphrase[x_members_and_y_guests]</phrase>)". Then create a template with whatever code you want and put the template name in the plugin. Code:
<br />Invisible Users: $var |
#7
|
|||
|
|||
That is incredibly helpful, thank you so much.
So something like this? Code:
$search_text = '$vbphrase[x_members_and_y_guests]</phrase>)'; $vbulletin->templatecache['FORUMHOME'] = str_replace($search_text, $search_text.fetch_template('silly_newbie_template'),$vbulletin->templatecache['FORUMHOME']); Code:
$vbphrase[x_members_and_y_guests]</phrase>) Invisible: $numberinvisible [high]* Guest210212002 thanks everyone for taking the time to help him out. [/high] Edit: I'm reading Kerry-Anne's post here on how to go about actually creating the template. https://vborg.vbsupport.ru/showthread.php?p=1018718 So I know I have to include this somewhere: Code:
eval('$mytemplate = "' . fetch_template('silly_newbie_template') . '";'); Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <product productid="chris777_showinvisible" active="1"> <title>Show Invisible Users Online</title> <description>Will add a count of invisible users in WGO.</description> <version>1.0.0</version> <dependencies></dependencies> <codes></codes> <templates> <template name="silly_newbie_template" templatetype="template" username="chris-777" version="1.0.0"> <![CDATA[ <!-- Newbie Template --> $vbphrase[x_members_and_y_guests]</phrase>) Invisible: $numberinvisible <!-- /Newbie --> ]]> </template> </templates> <plugins> <plugin active="1" executionorder="1"> <title>Show Invisible in WOL</title> <hookname>forumhome_loggedinuser</hookname> <phpcode><![CDATA[ $search_text = '$vbphrase[x_members_and_y_guests]</phrase>)'; $vbulletin->templatecache['FORUMHOME'] = str_replace($search_text, $search_text.fetch_template('silly_newbie_template'),$vbulletin->templatecache['FORUMHOME']);]]> </phpcode> <phrases> <phrasetype name="GLOBAL" fieldname="global"> <phrase name="online_invisible"> <![CDATA[ Invisible: ]]> </phrase> </phrasetype> </phrases> </product> This (I think) should make the template: Code:
<templates> <template name="silly_newbie_template" templatetype="template" username="chris-777" version="1.0.0"> <![CDATA[ <!-- Newbie Template --> $vbphrase[x_members_and_y_guests]</phrase>) Invisible: $numberinvisible <!-- /Newbie --> ]]> </template> </templates> Code:
<phpcode><![CDATA[ $search_text = '$vbphrase[x_members_and_y_guests]</phrase>)'; $vbulletin->templatecache['FORUMHOME'] = str_replace($search_text, $search_text.fetch_template('silly_newbie_template'),$vbulletin->templatecache['FORUMHOME']);]]> </phpcode> [high]* Guest210212002 grovels [/high] - Since I'm search/replacing, do I need to use forumhome_loggedinuser for the hook? Or should I use cache_templates? - Do I need to specify executionorder if there's only one plugin in the product? - Am I at least getting close, or should I just give up now? :bunny: Thanks very much again, I really do appreciate it. |
#8
|
||||
|
||||
Your 'newbie template' should just have whatever html you'd like to add. The string used in the plugin is a sort of locator, whatever you've got in the template is added beneath the string, it doesn't replace it -- so you can just put in the "Invisible Users: bla" stuff.
You don't need to declare your template in the plugin code, just put its name in the plugin used to find the string. I'd just use forumhome_complete for the plugin, that's what works for me, but feel free to try different hooks. But since you've mentioned caching templates, I just remembered that you'll want to do just that to avoid additional queries. Try this code in a plugin using hook 'cache_templates': Code:
if (THIS_SCRIPT == 'index'){ $globaltemplates = array_merge($globaltemplates, array('silly_newbie_template')); } I'd say you're getting close -- this is how we learn, heck this is how I learned, just posting a lot of threads in the Programming forum. |
#9
|
||||
|
||||
You can also just get the silly_newbie_template template from the cache provided you use a plugin to cache it, which you should. Then you won't need to run the fetch_template code.
PHP Code:
|
#10
|
|||
|
|||
Why can't you use a simple variable instead of those functions?
PHP Code:
|
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|