Can I run custom hooks from a plugin?
#1
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!
-TOA
Ordo Ab Chao
"Insanity: doing the same thing over again and expecting different results." - Einstein
Reply
#2
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
Reply
#3
(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
-TOA
Ordo Ab Chao
"Insanity: doing the same thing over again and expecting different results." - Einstein
Reply
#4
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);
Reply
#5
@Omar surely there's no need to do it that way? Since you're passing it by reference anyway...
Reply
#6
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.
Reply
#7
Ah, fair enough. So long as it works is all that matter xD
Reply
#8
Smile Thanks guys!
-TOA
Ordo Ab Chao
"Insanity: doing the same thing over again and expecting different results." - Einstein
Reply
#9
(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.
Random Fish and Sims Maniac
MY PLUGINS
Help MyBBSupport help you - remember to mark your threads as solved


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


Forum Jump:


Users browsing this thread: 1 Guest(s)