MyBB Community Forums

Full Version: What do I do?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Every plugin I see approaches things differently. For example, to display the content some use str_replace, or some use eval() and a few other things.

If I want to display a form or something when the hook is called, how do I do so? Let's say there's already a table, can I concatenate another row onto it or what? I need some guidance please!

Regards,
Shannon
The best method is to eval a template. The str_replace method is used when one wants to add something to the header whilst using the template cache (as there's no suitable hook for doing so otherwise).
So let's say I want to add a textbox and a label in the edit options page in User CP how do I do that?

I have the following hook:
$plugins->add_hook("usercp_options_start", "currentactivity_options");

How do you guys all know what to do without getting help?
Use find_replace_templatesets() in the activate function to add a variable to the edit options template. Then, in your currentactivity_options() function use eval like so:

global $myVariable;
eval("\$myVariable = \"".$templates->get('my_new_template')."\";");
Am I using the right hook and how do I use the find_replace_templatesets() function, is there some sort of documentation for it somewhere?

I need more insight. Sad
Download one of my plugins and take a look at how I do it - it should be fairly clear. There isn't really any great documentation around right now that I know of, though I am planning a simple guide for MyBBRox in the near future detailing the basic process of plugin creation.
Which do you think will be most relevant to what I'm wanting to do? I looked at MyStatus but it's right over the top of my head. Maybe a more minimalistic plugin would be helpful.

What do you suggest?
My last.fm plugin should be more easy to understand as it does far less: http://mods.mybb.com/view/last-fm-stats-for-mybb
Yeah, I was just taking a look at that.

For some reason, I can insert a template to the template table but where do I find it in the theme templates in the Admin CP? I just want to make sure everything is right. Also what is sid and what's its relevance?

I think I'm getting somewhere at least. This is where I am at the moment, I don't want you to do any code for me but just guide me a little more so I can finally achieve my first plugin. Smile

<?php
if (!defined("IN_MYBB")) {
	die("Direct initialisation of the script is not permitted.");
}

$plugins->add_hook("postbit", "currentactivity");
$plugins->add_hook("usercp_options_start", "currentactivity_options");

function currentactivity_info() {
	return array(
		"name" => "Current Activity",
		"description" => "Lists the current activity of a user on their profile and in the postbit.",
		"website" => "http://jrpgclub.com/community",
		"author" => "Shannon Rothe",
		"authorsite" => "http://jrpgclub.com/community",
		"version" => "1.6.8"
	);
}

function currentactivity_activate() {
	//create current_act table, fill with UID and current_activity
	global $db;
	$query = $db->query("CREATE TABLE " . TABLE_PREFIX . "current_activity (
							id INT AUTO_INCREMENT PRIMARY KEY,
							uid INT,
							current_act VARCHAR(65),
							current_obj VARCHAR(65)
						)");
						
	$template = array(
		"title" => 'currentactivity',
		"template" => '<label>Currently: </label><select><option>Watching</option><option>Playing</option><option>Listening</option></select>',
		"sid"		=> "-2",
		"version"	=> "1.0",
		"dateline"	=> TIME_NOW
	);

	$db->insert_query("templates", $template);
	
	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("usercp_custom_profilefields", '#{\$customfields}#', "{\$customfields}\n{\$current_act}");
}

function currentactivity_deactivate() {
	global $db;
	$query = $db->query("DROP TABLE " . TABLE_PREFIX . "current_activity");
	
	$db->delete_query("templates", "title='currentactivity'");
}

function currentactivity_options() {
	
}

function currentactivity() {

}

?>
The template will by default just go into the global templates section ACP > Templates > Global Templates. That all looks fine so far. You'll obviously want to undo the template edit in the deactivate function. In the currentactivity() function, you'll have something like this:

global $templates, $current_act;
eval("\$current_act = \"".$templates->get('currentactivity')."\";");

Also, I tend to prefix all templates in my plugins with the plugin name. It makes it easier to distinguish and avoids duplicating templates created by other plugins.
Pages: 1 2 3 4