MyBB Community Forums

Full Version: Put data into a variable using a plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello

So I want to explore with the plugin system and create a table that will go just above the forum (Asif it were a shoutbox).

I'm having troubles doing that... I managed to make a variable in the index template, but I'm not sure how to print data on that variable in a plugin. doing echo in PHP will output the data on the very top of the forums.

Also, I created another template and stored that successfully. I'd like to print that template on that variable, how can it be done?

I've tried googling but got nothing...

Thank you.
Have you tried Template conditionals? Or even the other one, PHP in templates...
$plugins->add_hook('global_end', 'do_print');
function do_print()
{
	global $header, $templates;

	eval('$do_print = "'.$templates->get('do_print').'";');

	$header = str_replace('<!--do_print-->', $do_print, $header);
}

This assuming the 'do_print' template exists and "<!--do_print-->" in found within the header code.

Please read:
http://community.mybb.com/thread-137925.html
http://mybbsource.com/thread-4003.html
http://mybbhacks.zingaburga.com/showthread.php?tid=74
(2014-04-05, 02:51 AM)Omar G. Wrote: [ -> ]
$plugins->add_hook('global_end', 'do_print');
function do_print()
{
	global $header, $templates;

	eval('$do_print = "'.$templates->get('do_print').'";');

	$header = str_replace('<!--do_print-->', $do_print, $header);
}

This assuming the 'do_print' template exists and "<!--do_print-->" in found within the header code.

Please read:
http://community.mybb.com/thread-137925.html
http://mybbsource.com/thread-4003.html
http://mybbhacks.zingaburga.com/showthread.php?tid=74

Thanks for your input! Though it's not working, maybe I'm doing something wrong.

Where did "$header" come from? I see it in global, but using that still doesn't work.
And also, I have stored a variable in the index template which is something like "{$myTestVar}"

It's not printing out anything Sad

Thanks.
(2014-04-05, 12:16 PM)Nick magic23 Wrote: [ -> ]
(2014-04-05, 02:51 AM)Omar G. Wrote: [ -> ]
$plugins->add_hook('global_end', 'do_print');
function do_print()
{
	global $header, $templates;

	eval('$do_print = "'.$templates->get('do_print').'";');

	$header = str_replace('<!--do_print-->', $do_print, $header);
}

This assuming the 'do_print' template exists and "<!--do_print-->" in found within the header code.

Please read:
http://community.mybb.com/thread-137925.html
http://mybbsource.com/thread-4003.html
http://mybbhacks.zingaburga.com/showthread.php?tid=74

Thanks for your input! Though it's not working, maybe I'm doing something wrong.

Where did "$header" come from? I see it in global, but using that still doesn't work.
And also, I have stored a variable in the index template which is something like "{$myTestVar}"

It's not printing out anything Sad

Thanks.

$header = the header template (stored itself as a variable). if you wish to add a variable from your plugin return it to the required template from a hook call .... if you are trying to add a variable without a plugin template conditionals plugin or xthreads is the way to go
(2014-04-04, 10:07 PM)Nick magic23 Wrote: [ -> ]So I want to explore with the plugin system and create a table that will go just above the forum (Asif it were a shoutbox).

I'm assuming you are writting a plugin.

If you want to include a variable within the "index" template, use the "index_end" hook. Something around the lines of:
$plugins->add_hook('index_end', 'do_print');
function do_print()
{
	global $templates, $my_var_foo;

	eval('$my_var_foo = "'.$templates->get('do_print').'";');
}

{$my_var_foo} should be in your "index" template and "do_print" template must exists with.
Omar, thank you! it worked!

- I have one more question though; in reguards to Javascript, "new Ajax.Request",

I want to send some data to PHP, so I made a hook connecting to xmlhttp.php and a function of course to it.

		new Ajax.Request('./xmlhttp.php?action=sixpackdharmiee', {method: 'post', postBody: shout, onComplete: function(request) {
			alert("Ok, sixPackDharmiee, check yo DB :) " + request);
		}});

this part works fine, however when alert is called, the request variable prints "[object Object]", what's causing that to?

Also, how would I fetch the data in PHP? I've tried $mybb->input['shout'] but it's returning nothing. And yes, I've added $mybb to global.

Thank you.
(2014-04-07, 11:21 AM)Nick magic23 Wrote: [ -> ]the request variable prints "[object Object]", what's causing that to?

request is an object that contains multiple bits of info, not a single string. Documentation of the different properties is here: http://prototypejs.org/doc/latest/ajax/A...index.html

(2014-04-07, 11:21 AM)Nick magic23 Wrote: [ -> ]Also, how would I fetch the data in PHP? I've tried $mybb->input['shout'] but it's returning nothing. And yes, I've added $mybb to global.

This I'm not 100% sure about, hopefully someone else can provide input, otherwise, I'd take a look at an existing plugin that makes AJAX requests and see how it does them.
(2014-04-07, 11:50 AM)Cameron:D Wrote: [ -> ]
(2014-04-07, 11:21 AM)Nick magic23 Wrote: [ -> ]the request variable prints "[object Object]", what's causing that to?

request is an object that contains multiple bits of info, not a single string. Documentation of the different properties is here: http://prototypejs.org/doc/latest/ajax/A...index.html

Yeah, I soon realised afterwords! Thank you!

However I still need help with the sending data via ajax. I tried looking at other plugins but the best I got was "undefined" :/
new Ajax.Request('./xmlhttp.php?action=sixpackdharmiee', {method: 'post', postBody: shout, onComplete:function sixpackdharmiee(request)
{
	if(error = request.responseText.match(/<error>(.*)<\/error>/))
	{
		alert("An error occurred when rating the post.\n\n" + error[1]);
		return false;
	}

	content = request.responseText.split('|-_-|');
	if(content[0] != 'success')
	{
		alert('An error occurred when rating the post.');
		return false;
	}

	alert('Ok, sixPackDharmiee, check yo DB :)');
	return true;
}});
return false;

Hope this does helps, JavaSciprt and AJAX is not on my skills :p
Pages: 1 2