MyBB Community Forums
Can I run custom hooks from a plugin? - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Development (https://community.mybb.com/forum-68.html)
+---- Thread: Can I run custom hooks from a plugin? (/thread-127014.html)

Pages: 1 2


Can I run custom hooks from a plugin? - TOA - 2012-10-02

I tried adding a custom hook to one of my plugins so I can add stuff to the page with other plugins. I'm having no luck getting anything to work.

Am I able to call custom hooks from a plugin? Or just regular hooks even?

Here's my code where I want to run the hook
$plugins->add_hook("misc_start", "pillars_page");
function pillars_page() {
	global $mybb, $db, $lang, $theme, $templates, $headerinclude, $header, $footer, $plugins;	
	
	if ($mybb->input['action'] != 'pillars') {
		return;	
	}
	
	$data .= 'Exercise<br />';
	$data .= $plugins->run_hooks("exersice_start");
	$data .= "After hook<br />";
	
	// add content to the template
	eval("\$content = \$data;");
	
	// output the page
	eval("\$page = \"".$templates->get("pillars")."\";");
	output_page($page);
}

And here's the code I'm trying to add to that hook
$plugins->add_hook("exercise_start", "custom_hook_test_code");
function custom_hook_test_code(&$data) {
// I've tried like 5 different variations of this and nothing seems to work
	global $data;
	
	$data = str_replace("<br />", "<br /><p>Caught my hook!</p>", $data);
	return $data;
}

I get no errors, but no matter what I do in my other plugin, I can't get my content to display.

Thanks for reading.



SOLVED - Stupid spelling errors!


RE: Can I run custom hooks from a plugin? - Euan T - 2012-10-02

Try something like this:

$plugins->run_hooks('exersice_start', $data);

Then hook into it with

$plugins->add_hook("exercise_start", "custom_hook_test_code");
function custom_hook_test_code(&$data) {
    $data = str_replace("<br />", "<br /><p>Caught my hook!</p>", $data);
}

That should work Smile


RE: Can I run custom hooks from a plugin? - TOA - 2012-10-02

(2012-10-02, 03:46 PM)euantor Wrote: Try something like this:

$plugins->run_hooks('exersice_start', $data);

Then hook into it with

$plugins->add_hook("exercise_start", "custom_hook_test_code");
function custom_hook_test_code(&$data) {
    $data = str_replace("<br />", "<br /><p>Caught my hook!</p>", $data);
}

That should work Smile

Thanks! That does work.

It was a spelling error; I mis-spelled 'exercise' in the run_hooks call Blush


RE: Can I run custom hooks from a plugin? - Omar G. - 2012-10-02

Unless your variable is in the global scope, you need to pass it by reference, just as Euantor showed you.

Though, slighter different, I will do it like this:
$data = $plugins->run_hooks('exersice_start', $data);



RE: Can I run custom hooks from a plugin? - Euan T - 2012-10-02

@Omar surely there's no need to do it that way? Since you're passing it by reference anyway...


RE: Can I run custom hooks from a plugin? - Omar G. - 2012-10-02

Yep, there is no real need Toungue MyBB does it that way in most places (that I know of) so I do it that way for consistency.


RE: Can I run custom hooks from a plugin? - Euan T - 2012-10-02

Ah, fair enough. So long as it works is all that matter xD


RE: Can I run custom hooks from a plugin? - TOA - 2012-10-02

Smile Thanks guys!


RE: Can I run custom hooks from a plugin? - Leefish - 2012-10-02

(2012-10-02, 04:54 PM)Omar G. Wrote: Unless your variable is in the global scope, you need to pass it by reference, just as Euantor showed you.

Sorry to hijack, but what do you mean by global scope Omar? I have seen this referred to a couple of times now.


RE: Can I run custom hooks from a plugin? - Euan T - 2012-10-02

By global scope, he means that the variable is accessible via the $GLOBALS superglobal array I believe.