MyBB Community Forums

Full Version: Need help, making a new plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hey everyone,

I am making a plugin called ColourChanger that adds buttons to change the background colour of the theme.
But I am getting an error.
It uses javascript, so I need HTML code to be inputted.
This is my current code:
<?php
/**
 * ColourChanger 1.0
 * Aniruddh
 * Project Started: 21 April 2012
 * Project Finished:
 */
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly");
} 

function colourchanger_info()
{
return array(
        "description"=> "A simple plugin which uses javascript to change the background of your forums",
        "name"  => "Colour Changer",
        "author"        => "Aniruddh",
        "version"        => "v1.0",
        "compatibility" => "16*"
    ); 
} 
  ob_start();
  ?>
  <script language="JavaScript">
<!--
function changeBGC(color){
	document.bgColor = color;
}
//-->
</script>
<a href="#" onClick="javascript:changeBGC('#0697d3')"><img src="images/Buttons/Button_Blue.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Green.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Yellow.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Red.png"></a><br />
<?php
$buttons = ob_get_contents();
ob_end_flush();
 function colourchanger_activate()
 {
 global $db, $mybb;
 	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets(
		"index",
		'#'.preg_quote('{$header}').'#',
		'{$header}{$buttons}'
	);		
 
 }
 ?>

My output:
[Image: Result.png]
As you can see, the buttons are displayed on the top, although they were intended to be displayed after the header. And they are not working .
And strangely, in the AdminCP:
when the plugin is deactivated:
[Image: AdminCPDeactivated.png]
when the plugin is activated:
[Image: AdminCPActivated.png]

Please help me out.

Thanks!
Aniruddh
There are quite a few things wrong from what I can see.

First, there is no deactivate function... so when the plugin is deactivated it will not revert the template.

Second, I would not suggest using ob_start() in this case. It's simply unorthodox and unnecessary if you ask me. Why not simply store that into a variable to begin with? On a related note, it's being displayed because you have ob_end_flush().. to prevent that from showing (I think you need to get rid of it all together) you'd need to use ob_end_clean().

Third, you need a hook to trigger a new function that tells it what to do. If you are wanting this to show up on every page you will probably need to use the global_start hook.

Ex:
$plugins->add_hook("global_start", "DoStuff");
function DoStuff(){
    //here you would assign your html and eval your {$buttons} variable, ect...
}


I hope this makes sense. It's late here. I will check this thread tomm and hopefully you have it figured out.
I know there is no deactivate function, because the activate function is not working right.
I tried adding the HTML code to the variable in this way:
$buttons = "<script language="JavaScript">
<!--
function changeBGC(color){
	document.bgColor = color;
}
//-->
</script>
<a href="#" onClick="javascript:changeBGC('#0697d3')"><img src="images/Buttons/Button_Blue.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Green.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Yellow.png"></a><br />
<a href="#" onClick="javascript:changeBGC('#000099')"><img src="images/Buttons/Button_Red.png"></a><br />"
Well it did not workout correct, due to the inverted commas in the code too.

Is there another way I can do that? I was thinking of adding the code in a separate html file, but then I got confused how would I add the data of the HTMl file.
Please help me out.
Two ways: Either escape your double quotes... or change them to single quotes.
Example:
$buttons = "<script language=\"JavaScript\">

or

$buttons = "<script language='JavaScript'>

ect.. through out the whole string.


Edit: Since you have some single quotes too.. it'd prob just be easier to escape them. Here:
$buttons = "<script language=\"JavaScript\">
<!--
function changeBGC(color){
    document.bgColor = color;
}
//-->
</script>
<a href=\"#\" onClick=\"javascript:changeBGC('#0697d3')\"><img src=\"images/Buttons/Button_Blue.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Green.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Yellow.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Red.png\"></a><br />"
(2012-04-21, 06:38 AM)KoBE Wrote: [ -> ]Two ways: Either escape your double quotes... or change them to single quotes.
Example:
$buttons = "<script language=\"JavaScript\">

or

$buttons = "<script language='JavaScript'>

ect.. through out the whole string.
Are you sure it will still work?
You should use global hook only when required. Because if its a more active forum, there'd be numerous number of queries running out there.
(2012-04-21, 06:39 AM)Aniruddh Wrote: [ -> ]
(2012-04-21, 06:38 AM)KoBE Wrote: [ -> ]Two ways: Either escape your double quotes... or change them to single quotes.
Example:
$buttons = "<script language=\"JavaScript\">

or

$buttons = "<script language='JavaScript'>

ect.. through out the whole string.
Are you sure it will still work?
I edited the post.. check out what I did.

(2012-04-21, 06:43 AM)crazy4cs Wrote: [ -> ]You should use global hook only when required. Because if its a more active forum, there'd be numerous number of queries running out there.

I agree, but if this is something he is wanting displayed on every page then it's what he'll have to do. He isn't querying the database, so even on large forums this would be minimal.
(2012-04-21, 06:44 AM)KoBE Wrote: [ -> ]
(2012-04-21, 06:39 AM)Aniruddh Wrote: [ -> ]
(2012-04-21, 06:38 AM)KoBE Wrote: [ -> ]Two ways: Either escape your double quotes... or change them to single quotes.
Example:
$buttons = "<script language=\"JavaScript\">

or

$buttons = "<script language='JavaScript'>

ect.. through out the whole string.
Are you sure it will still work?
I edited the post.. check out what I did.

(2012-04-21, 06:43 AM)crazy4cs Wrote: [ -> ]You should use global hook only when required. Because if its a more active forum, there'd be numerous number of queries running out there.

I agree, but if this is something he is wanting displayed on every page then it's what he'll have to do. He isn't querying the database, so even on large forums this would be minimal.
OO thanks man, I will surely include your name in the credits when the plugin is released Smile
Eh, no problem. Glad you got it working. I hope you understand \'why\' it works... not just 'that' it works. Big Grin
Ok so I did everything, but its still not being displayed Sad
My code:
<?php
/**
 * ColourChanger 1.0
 * Aniruddh Agarwal
 * Project Started: 21 April 2012
 * Project Finished:
 */
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly");
} 

$plugins->add_hook("global_start", "colourchanger_activate");

function colourchanger_info()
{
return array(
        "description"=> "A simple plugin which uses javascript to change the background of your forums",
        "name"  => "Colour Changer",
        "author"        => "Aniruddh",
        "version"        => "v1.0",
        "compatibility" => "16*"
    ); 
} 
 function colourchanger_activate()
{
$buttons = "<script language=\"JavaScript\">
<!--
function changeBGC(color){
    document.bgColor = color;
}
//-->
</script>
<a href=\"#\" onClick=\"javascript:changeBGC('#0697d3')\"><img src=\"images/Buttons/Button_Blue.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Green.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Yellow.png\"></a><br />
<a href=\"#\" onClick=\"javascript:changeBGC('#000099')\"><img src=\"images/Buttons/Button_Red.png\"></a><br />";

global $db;
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets(
		"index",
		'#'.preg_quote('{$header}').'#',
		'{$header}{$buttons}'
	);
}

function colourchanger_deactivate()
{
global $db;
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets(
		"index",
		'#'.preg_quote('{$buttons}').'#',
		''
	);		
}
	
	

?>

Whats wrong now?
The activate and deactivate functions add and remove the value {$buttons} after the header variable thing in the template all right, but the code is not being displayed!
What should I do?
Pages: 1 2 3