MyBB Community Forums

Full Version: Edit postbit with plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there. I'm working on a plugin which works around the Postbit area. I have successfully added a variable to the postbit template and using the postbit hook too.

However, what ever I put in the variable, it doesn't print it out? Here is my code.

<?php
// My plugin lol.

if(!defined("IN_MYBB")) {
	die("Gtfo lol");
}

$plugins->add_hook("postbit", "test_work");

function test_info() {
	return array(
		"name"			=>	"test name",
		"description"	=>	"test desct.",
		"author"		=>	"Dharmesh Makwana",
		"version"		=>	"1",
		"compatibility"	=>	"16*"
		);
}

function test_activate() {
	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("postbit", "#" . preg_quote("{\$post['groupimage']}") . "#", "{\$post['groupimage']}\n{\$displayAdditionalGroups}");
}

function test_deactivate() {
	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("postbit", "#\n" . preg_quote("{\$displayAdditionalGroups}") . "#", "");
}

//Dis is da magik.
function test_work() {
	$displayAdditionalGroups = "M ADD";
}

If anyone can tell me what I'm doing wrong, I'd appreciate it.
I'm not sure (I'm beginner) but I think that you need to add templates to a global list. Something like:
//Dis is da magik.
function test_work() {
    global $mybb, $templates; 
    $displayAdditionalGroups = "M ADD";
}
- indeed, correct solution is given below by Destroy666

L.E.: (off-topic) what's happen with this thread (I see over 1500 views in only 5-10 minutes)? Sad
//Dis is da magik.
function test_work(&$post) {
    $post['something'] = "M ADD";
    return $post;
}

Then output $post['something'] in one of postbit templates.
(2013-11-13, 10:47 PM)Destroy666 Wrote: [ -> ]
//Dis is da magik.
function test_work(&$post) {
    $post['something'] = "M ADD";
    return $post;
}

Then output $post['something'] in one of postbit templates.

Hey, thank you for your contribution! But can you tell me why I can't use any other variable? And why do I need to add &$post to the function?

I'd just like to know because it may help me in the future. Thanks!

(2013-11-13, 09:14 PM)Flavius Popa Wrote: [ -> ]L.E.: (off-topic) what's happen with this thread (I see over 1500 views in only 5-10 minutes)? Sad

IKR? This thread has 30k views now. Jessus.

EDIT:

For some reason, using my own variable {$post['dag']} for example, which is created via my plugin using the find_replace_templatesets function, It wont print out text that I set. However, using the variable {$post['groupimage']} I appended my text and that worked using .= assignment operator in PHP.

Got any ideas why I can't use my own variable? IT even prints on the template :/
I tested my solution and it works. Maybe you check wrong postbit (you insert it only to MyBB's default, not classic)?

Here whole plugin:
<?php
// My plugin lol.

if(!defined("IN_MYBB")) {
    die("Gtfo lol");
}

$plugins->add_hook("postbit", "test_work");

function test_info() {
    return array(
        "name"            =>    "test name",
        "description"    =>    "test desct.",
        "author"        =>    "Dharmesh Makwana",
        "version"        =>    "1",
        "compatibility"    =>    "16*"
        );
}

function test_activate() {
    require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
    find_replace_templatesets("postbit", "#" . preg_quote("{\$post['groupimage']}") . "#", "{\$post['groupimage']}{\$post['something']}");
}

function test_deactivate() {
    require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
    find_replace_templatesets("postbit", "#" . preg_quote("{\$post['something']}") . "#", "");
}

//Dis is da magik.
function test_work(&$post) {
    $post['something'] = "<strong>This code works</strong>";
    return $post;
}
?>
Result: https://docs.google.com/file/d/0B6pgReiH...p=drivesdk

Only mistake I made in previous post the "one of postbit templates" because it can be interpreted as "in all postbit templates". But it won't work in postbit_author_user, for instance, because of the hook placement. Then you need to use str_replace or similar method. It surely works in postbit/postbit_classic templates.

About &$post - the variable needs to be passed by reference because the hook is called for every post.
Ahh yeah, of course Smile

Anyway, just like to thank you personally for your help. Much appreciated.

The base of my plugin has been completed Smile
[Image: ZdqzWoe.png]
to use this plugin what should i name it i mean code is here but i should do postbit.php?