MyBB Community Forums

Full Version: Plugins - How to use hooks?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Just wondering what you guys thought of as good plugin ideas, both big and small.

I just want to practice and start to develop some actual skills, so wanted some ideas about what would be good plugin ideas.

Look out for requests in request forum, that is how I develop as per their needs. Just made two plugins based upon the request, awaiting activation.
I will do that thanks.

On the issue of actually building plugins, it's hard to actually find how hooks can be implemented. I mean i know what they do, but actually understanding how to use them is the difficult bit.
$plugins->run_hook('hook_name', 'hook_function');

When the hook called 'hook_name' is run in the MyBB code, your function 'hook_function' is called and run. You just write whatever code you want to be run in that function and it'll be run when that hook is called.
Thanks for the response Matt but im still a little confused.

Just taking a random hook:
postbit 

When would this be used and how do you know it needs to be ran?

Looking here: http://wiki.mybb.com/index.php/Plugins:The_Hook_System

Taking the example given there:
$plugins->add_hook("global_end", "foo_functionname");

What will global_end achieve?

I think the myBB documentation is good, it just lacks on the hooks side of things because i cannot actually practically implement the use of hooks.
Are you talking in reference to with my Autoban plugin which uses global_end hook? See, its very easy. You can relate it with what you want to do. If you make some plugin which does some edit in postbit area, you use postbit related hooks, if it runs globally, like in header, you use the global hooks, if it uses newreply.php, you can use new_reply_start or new_reply_do_start (processed and checks query after thread is posted), hope it makes clear.
Definitely helps yes thanks. And no, i took the global_end hook from the examples given here: http://wiki.mybb.com/index.php/Plugins:The_Hook_System

So for example, if i wanted to make a plugin that would put a word on the index page (a word that could be typed in from the settings in the ACP), i would need

index_start
hook?

And then to add a settings in the ACP:

//Word to go on index
$set_text = array(
		"name" => "set_text",
		"title" => "Type the word to go on the index.",
		"description" => "What should be shown on the index?",
		"optionscode" => "textarea",
		"value" => "",
		"disporder" => "1",
		"gid" => .....(SOMETHING HERE?)
		);
	$db->insert_query("settings", $set_text);

Hows that?
There are couple of ways to do that. One is that you could insert your variable into index page template and then declare your conditions or you could use something like:

if(THIS_SCRIPT == "index.php") {

//do your thing or write a condition
}

For example, you're saying to display text that a user writes in textbox of your setting, you could do something like:

$setting1 = array(
		"sid"			=> NULL,
		"name"			=> "setting1",
		"title"			=> "Enter",
		"description"	=> "Enter something below:.",
		"optionscode"	=> "textarea",
		"value"			=> "Default text.",
		"disporder"		=> 1,
		"gid"			=> intval($gid)
	);

	$db->insert_query("settings", $setting1);

And then to output it on index page, you could use something like:

if(THIS_SCRIPT == "index.php") {

{$mybb->settings['setting1']}
}

So that would return the value entered by user in textbox, however I've never worked a lot with THIS_SCRIPT, I prefer to use templates and hooks but at certain places, you need to use that.

edit: Also I'll advice to look at my below plugin for ref as it does the similar thing you're asking:

http://mods.mybb.com/view/modcp-rules
Thanks crazy, ill definitely look.
Pages: 1 2