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
  #2  
Old 06-09-2005, 05:01 PM
amykhar's Avatar
amykhar amykhar is offline
 
Join Date: Oct 2001
Location: PA
Posts: 4,438
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks, Brad. It's nice to have a plugins for dummies tutorial to get started with
Reply With Quote
  #3  
Old 06-09-2005, 05:49 PM
Logikos Logikos is offline
 
Join Date: Jan 2003
Posts: 2,924
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent! Now i understand the hooks system.
Reply With Quote
  #4  
Old 06-09-2005, 05:55 PM
amykhar's Avatar
amykhar amykhar is offline
 
Join Date: Oct 2001
Location: PA
Posts: 4,438
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm glad somebody does

The first hack I looked at porting doesn't look like it will be able to use the hook system, unfortunately. I really wish jelsoft had come up with a more extensible way to format queries. If the queries have to be changed, we run into the same problems we had before.

I would love it if the arguments to a query were set up in an array format that could be easily added to. That way, if we are forced to add into an existing query, the user could modify the arguments to the query rather than the query itself. It would be so much easier to prevent hack conflicts and installation errors that way.

Amy
Reply With Quote
  #5  
Old 06-09-2005, 05:58 PM
Brad Brad is offline
 
Join Date: Nov 2001
Posts: 4,765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Amy: If you see a good spot for a hook request it at vBulletin.com, this is one of the main reasons we are encourged to hack the beta
Reply With Quote
  #6  
Old 06-09-2005, 06:02 PM
Logikos Logikos is offline
 
Join Date: Jan 2003
Posts: 2,924
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Brad i have a question, I ported my icons hack. But i have to hack the admincp/forum.php for the option to show up in the admincp. Does the Hooks system work in the admincp/ dir? From what i read in the code it doesn't.

Maybe you have another solution to this.

Its just this:
PHP Code:
print_textarea_row('description''forum[description]'$forum['description']); 
Reply With Quote
  #7  
Old 06-09-2005, 06:03 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Unfortunately there are currently no hooks within the AdminCP.
Therefor you will have to modify the files.
Reply With Quote
  #8  
Old 06-09-2005, 06:07 PM
Logikos Logikos is offline
 
Join Date: Jan 2003
Posts: 2,924
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by KirbyDE
Unfortunately there are currently no hooks within the AdminCP.
Therefor you will have to modify the files.
Then whats the point! Bah, so much for that...
Reply With Quote
  #9  
Old 06-09-2005, 06:09 PM
Brad Brad is offline
 
Join Date: Nov 2001
Posts: 4,765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Live Wire
Then whats the point! Bah, so much for that...
Less file modification = easier to upgrade.

Like I said, you see a useful place for a hook request one at vBulletin.com!
Reply With Quote
  #10  
Old 06-09-2005, 06:09 PM
amykhar's Avatar
amykhar amykhar is offline
 
Join Date: Oct 2001
Location: PA
Posts: 4,438
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I am not sure a hook would help my problem. For example, if you look at index.php, you see a query to get the logged in users.

Code:
$forumusers = $db->query_read("
		SELECT
			user.username, (user.options & " . $vbulletin->bf_misc_useroptions['invisible'] . ") AS invisible, user.usergroupid,
			session.userid, session.inforum, session.lastactivity,
			IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
		FROM " . TABLE_PREFIX . "session AS session
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
		WHERE session.lastactivity > $datecut
		" . iif($vbulletin->options['displayloggedin'] == 1, "ORDER BY username ASC") . "
	");
Now, I need to know the useragent field from the sessions table. So, I have to change the query to:

Code:
$forumusers = $db->query_read("
		SELECT
			user.username, (user.options & " . $vbulletin->bf_misc_useroptions['invisible'] . ") AS invisible, user.usergroupid,
			session.userid, session.inforum, session.lastactivity,session.useragent,
			IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
		FROM " . TABLE_PREFIX . "session AS session
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
		WHERE session.lastactivity > $datecut
		" . iif($vbulletin->options['displayloggedin'] == 1, "ORDER BY username ASC") . "
	");
I don't think a hook would change that for me. But, if each query had it's arguments defined in a structure that defined the columns to read, update, or insert and had a way to set up joins, etc. outside the query in a clean, easy to modify way (such as adding arguments to an array), then we could hack right in there without worry of conflicts or sql syntax issues.

Amy
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 07:22 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.12153 seconds
  • Memory Usage 2,320KB
  • Queries Executed 25 (?)
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
  • (6)bbcode_php
  • (4)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_postinfo_query
  • fetch_postinfo
  • 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