MyBB Community Forums

Full Version: Common Errors when making a plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Heloo there,

I'd like to state here some common errors that you might encounter when making your own plugins, a little bit of my little experience in plugins for those who are new.

1- Correct & Common naming

When making a plugin you should ensure to have a common name for the plugin and for the activation , deactivation, info etc...functions.
what i mean in. if you make for example

function icons_activate()

your plugin file should be called icons.

or you will not find your plugin in the plugin manager, or the plugin will not work.

2- Escaping all joker characters like $, [, ], ?, ( and ).
When making a plugin and u miss escaping the jokers, you'll plugin will not do the job when activated. this is only apllied for the find string in the function find_replace_templatesets.

What i mean by escaping is
in case u have

Quote:$mybbuser[avatar]

you have to escape it using "\"

Quote:\$mybbuser\[avatar\]

3-When having multilines
It's better that in the search part of the replace templateset, that you dont use multipule lines. however if u have multipule lines make sure to include the letter "s" after the #
for example

"#<td valign="top" align="right">
<div class="bottommenu"><span class="smalltext">\$logoutlink</td>#s"


4-Watch up not to use "0" in activate
You dont incluse "0" at the end of the activation for a certain change of templates. As 0 means you don't want to create a new template. however if you change something, you must create a new template otherwise reverting to original won't be possible.
so only use it for deactivating


umm in case others would like to add things here you're freeSmile


bbye

I didnt know about #3. cool Big Grin
Just to correct and add to the above

2- Escaping all joker characters like $, [, ], ?, ( and ).
You should NOT escape all the characters in your plugin, just the ones used for the find string in the function find_replace_templatesets

I think the above poster is referring to the function find_replace_templatesets. This function will basically search a specified template with the search string and replace it with something else. The function uses the function preg_replace to look for code. This means you have to 'escape' certain characters. Most noticeably $ and all of the brackets ([{}]). There are a few others but these are the main ones you need to know about.

You should use the function preg_quote to do all of this work for you.
You should also store your search strings in string values instead of copying/pasting the code into the relevant places. If you want to search for something and add something after the search string, some people might use
require_once("inc/adminfunctions_templates.php");
function plugin_activate(){
find_replace_templatesets('postbit', '#reputation#', 'reputation<br/>mystring');
}
function plugin_deactivate(){
find_replace_templatesets('postbit', '#<br/>mystring#', '', 1);
}
it makes more sense to store the things in strings like so
$find_string = "reputation";
$replace_string = "<br/>mystring";
require_once("inc/adminfunctions_templates.php");
function plugin_activate(){
global $find_string, $replace_string;
find_replace_templatesets('postbit', '#'.preg_quote($find_string).'#', $find_string.$replace_string);
}
function plugin_deactivate(){
global $find_string, $replace_string;
find_replace_templatesets('postbit', '#'.preg_quote($replace_string).'#', '', 0);
}
Note the use of preg_quote. You won't have to manually add all of the extra slashes and if you change the code in one place, you won't have to change it anywhere else.


4-Watch up not to use "0" in activate
When you use the function find_replace_templatesets the last parameter is optional.
If it isn't used, or set to 1, it will create a new template from the 'master' template. This means an option will appear in the Admin CP for users to revert back to the original (default) template.
If set to 0 then it will try to edit all templates with that title.

So when you install/edit a template for the first time (read: activating a plugin) using the function find_replace_templatesets, you should omit this parameter so that it will create a copy of the master template you are trying to edit.
And when you are deactivating your plugin, you should set this parameter to 0. Like in the code above/below

$find_string = "reputation";
$replace_string = "<br/>mystring";
require_once("inc/adminfunctions_templates.php");
function plugin_activate(){
global $find_string, $replace_string;
find_replace_templatesets('postbit', '#'.preg_quote($find_string).'#', $find_string.$replace_string);
}
function plugin_deactivate(){
global $find_string, $replace_string;
find_replace_templatesets('postbit', '#'.preg_quote($replace_string).'#', '', 0);
}

Notice the extra parameter at the end of the function in plugin_deactive. That will make the find/replace work on all templates with that name.


I will add some more tips when my eyes are working.
yes sorry regarding the 2nd one
i forgot to mention that Smile yes it's only for the find string in the function find_replace_templatesets i'll edit my post Smile

ciao