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

Reply
 
Thread Tools
[How to] Write Plug-ins/Hooks
Brad
Join Date: Nov 2001
Posts: 4,765

 

Show Printable Version Email this Page Subscription
Brad Brad is offline 06-06-2005, 10:00 PM

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 .

[high]Things to consider[/high]

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!

[high]Basics:[/high]

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:

Quote:
[high]vbulletin_plugins.xml[/high]
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.
[high]Tips[/high]

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:

Quote:
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
Reply With Quote
  #62  
Old 08-15-2007, 11:12 AM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Fair enough, I don't see why that link I posted wouldn't work the same in a plugin. Just do the same but instead of writing the hook in the PHP file stick it in your plugin. No harm in trying if you haven't already done so

Did you get some kind of error or was it just not working?
Reply With Quote
  #63  
Old 08-15-2007, 11:21 AM
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Location: Greece
Posts: 1,856
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I didn't try it yet, as I didn't need it at the time (I need it today).
I'll try soon though and post what happened.
Reply With Quote
  #64  
Old 09-03-2007, 04:53 AM
amnesia623 amnesia623 is offline
 
Join Date: Jul 2006
Location: Glendale, AZ
Posts: 226
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Is this tutorial current as of vb 3.6.8?

I'm looking for a plugin tutorial and wondered if this is still relavent
Reply With Quote
  #65  
Old 09-21-2007, 03:42 PM
Birched Birched is offline
 
Join Date: Sep 2007
Posts: 35
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The advice by KirbyDE at the end of the main entry was BY FAR the most useful part for me, but yes, I believe it is all still relevant.
Reply With Quote
  #66  
Old 09-24-2007, 12:04 PM
Hubbitus Hubbitus is offline
 
Join Date: Nov 2005
Location: Russia
Posts: 6
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

At the beginning, thank you for the good basic guide.

But I'm not completely understudying how I can associate my hook with two or more "hookname" at once? E.g. "newreply_post_start", "newthread_post_start".
Quote:
Originally Posted by Brad View Post
Code:
<title>vB Category Icons</title>
<hookname>forumdata_start</hookname>
If I try this code:
Code:
<title>vB Category Icons</title>
<hookname>newreply_post_start</hookname>
<hookname>newthread_post_start</hookname>
I get hook, associated to "useradmin_edit_start", as I understood the first item at the list.

Is it the only way to make the fully copy of the code into a new hook and associate it with new hookname???
Reply With Quote
  #67  
Old 09-24-2007, 03:03 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This tutorial is old. You can add plugins through the AdminCP via the Plugin Manager.

I don't think it is possible to associate the same code to two hooks. Just copy the code over to a new plugin to create code for another hook.
Reply With Quote
  #68  
Old 09-24-2007, 06:00 PM
Hubbitus Hubbitus is offline
 
Join Date: Nov 2005
Location: Russia
Posts: 6
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Opserty View Post
This tutorial is old. You can add plugins through the AdminCP via the Plugin Manager.
Of cause, I known. But, I'm don't known how associate the same code to two hooks in this case too.

Quote:
Originally Posted by Opserty View Post
I don't think it is possible to associate the same code to two hooks. Just copy the code over to a new plugin to create code for another hook.
If no other, "normal" way, off cause i do that. But I hope that someone will prompt more correct way.
Reply With Quote
  #69  
Old 10-05-2007, 04:28 PM
hobeau hobeau is offline
 
Join Date: Sep 2007
Posts: 3
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi, I just want to display a list of users who have posted on a thread at the top of the thread using the hook system. I'm brand new to VBulletin and was wondering if there is a good tutorial or documentation that would help. I've looked in the vb manual and it just has like 2 pages on writing plugins. Can anyone help?
Reply With Quote
  #70  
Old 05-05-2009, 04:34 PM
wottech wottech is offline
 
Join Date: May 2006
Posts: 122
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Ken Iovino View Post
If you wanted to do this is 3.5 using hooks you can. Open showthread.php and find around line: 923-924. You should see...

PHP Code:
$hook_query_fields $hook_query_joins '';
(
$hook vBulletinHook::fetch_hook('showthread_query')) ? eval($hook) : false
Under that you will see a very large query, in that query you will notice the variables, $hook_query_fields, and $hook_query_joins. Thats your ticket into this query.

You would simple go into the plugins, add new plugin and write it like so: Plugin Name is "showthread_query"
PHP Code:
$hook_query_fields ",tablename.rowname AS newname";
$hook_query_joins "LEFT JOIN " TABLE_PREFIX "tablename AS newtable ON(tablename.rowname = post.rowname)"

Is there a way to add WHERE and ORDER statements to this query as well, using hooks?
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 06:20 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.04594 seconds
  • Memory Usage 2,321KB
  • Queries Executed 24 (?)
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
  • (7)bbcode_php
  • (6)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
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)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