Back to vBulletin 3 Articles

[How to] Write Plug-ins/Hooks
by Brad 07 Jun 2005
Rating: (2 votes - 4.50 average)

This is your basic guide to making plug-ins and hooks, as we are all coming over from the 'hack the files' mentality I am posting this thread more for people that are used to hacking the 3.0.x source code and looking to port modifications. Although a newbie should be able to come away with a good understanding of how to do this to .

Things to consider

Ok before we get started here are a few things to keep in mind. First and foremost plug-ins are stored in the database, serlized, unserlized and evaled on page generation. If you code preforms badly with such a wrapper forget making a plug-in and just hack it, trust me you'll thank yourself for it when your forum gets large.

Second thing, hooks will not get you into every corner of the code. Some things will always be done best with a hack. For example if you are looking to add something but need to query the db for some extra data and want to avoid and extra query (in other words you are going to modify an existing one with a join or such) forget it and hack it in, there are no hooks that let you modify existing queries.

Last but not least hooks are not magic, don't add a million of them and expect your forum to run as fast as it did when you first installed it. Only plug-in/hack-in what you really need!

Ok enough with the boring stuff, lets add our first plug-in!

Basics:

First thing you need to do is make sure plug-ins are enabled, you can find this option in the admin cp by browsing to vBoptions -> Select Plugin/Hook System from menu -> Set Enable Plugin/Hook System to yes.

Now head to the add plug-in page located at http://www.yoururl.com/forum/admincp/plugin.php?do=add

Lets go over what all these options mean:

Hook Location: This is where the php code will be executed at, hook locations at defined all across vBulletin. You can find them by opening and php file and searching for the var $hook. When you find a bit of php code like this you have found a hook:

PHP Code:
($hook vBulletinHook::fetch_hook('showthread_postbit_create')) ? eval($hook) : false
See that bit fetch_hook('showthread_postbit_create')? Well the text "showthread_postbit_create" is the name of this hook, if you look in the drop down you will find that listed in there somewhere. Incase you are wondering that hook is located in showthread.php at line 1012

Title: This is the title of your plug-in, use a good name because this is the only thing you have to identify the plug-in in the plug-in manager.

Plugin PHP Code: Can you guess? This is where you put your custom php code, on page generation it is executed at the hook location in the .php files. Note that you do not need <?php ?> tags here, in other words:

wrong:

PHP Code:
<?php $var true?>
Right:

PHP Code:
$var true
Plugin is Active: Allows you to turn the plug-in on/off without removing it. If set to 'yes' php code is ran on page execution.

Edit - Thanks to Revan for this addition:

vbulletin_plugins.xml
If you need to tell your users to add a lot of plugins, this will become tedious as much copy/pasting is required. A simpler way would be to use the .xml import. It works just like the importing of templates and phrases, by adding the contained code as a Plugin.
The correct format for a plugin.xml file is like so (thanks to Live Wire):
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<plugins>
	<plugin active="1">
		<title>vB Category Icons</title>
		<hookname>forumdata_start</hookname>
		<phpcode><![CDATA[$this->validfields['forumhomeicon'] = array(TYPE_STR, REQ_NO);]]></phpcode>
	</plugin>
</plugins>
Explanations:
<plugin active="1"> - The 'active' attribute determines the default value of 'Plugin is Active' in the Plugin Manager.
<title></title> - Self explanatory, it is the 'Title' field in the Manager.
<hookname></hookname> - The 'Hook Location' you would select.
<phpcode><![CDATA[ ]]></phpcode> - Anything added in the space between these is added to the 'Plugin PHP Code' part.
Tips

At first approach coding the plug-in like a hack.

Open the php file you would normally edit and get your mind around the new code.

Once you have a good understanding of the new code you should start looking for where you need to add your custom php. Once you find the right location in the code start looking for a nearby hook, if you don't see one you can work with you are out of luck!

Edit- Thanks to KirbyDE for his addition:

Something that might come in handy for those developing Plugins:

In class_hook.php FIND
PHP Code:
function &fetch_hook($hookname)

BELOW that ADD
PHP Code:
DEVDEBUG("Calling Hook $hookname"); 
Then (if debug mode is turned on) you can see which hooks are being called, and when they are being called.
Once you find your hook note its name and apply your changes in the admincp, from here on out you are on your own

vblts.ru supports vBulletin®, 2022-2024