MyBB Community Forums

Full Version: Can I run custom hooks from a plugin?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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!
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
(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
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);
@Omar surely there's no need to do it that way? Since you're passing it by reference anyway...
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.
Ah, fair enough. So long as it works is all that matter xD
Smile Thanks guys!
(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.
By global scope, he means that the variable is accessible via the $GLOBALS superglobal array I believe.
Pages: 1 2