MyBB Community Forums

Full Version: _activate function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I need to do a check in the activation function of a plugin so if the check is not passed the plugin would not be activated. How could I do that?.

What exactly sets a plugin in activation state? What must I have to change to deactivate it if the check fails?

Besides someone knows of some documention about hooks? Knowing only the names and the parameters it receives about the hooks is really a nightmare to try to code a plugin.
Just do a simple if within the activate function. If you want to do more, you need to use the install function and the _isinstalled function to do a check.

As for hook documentation, the only docs I know of say where they are and the name. Other than that, you can look in the files they're located in.
As soon as I click Install & Activate _install and _activate functions are called and if no errors the plugin is installed and activated.

The _activate function is called itself and it seems it sets the plugin in activate mode itself too. I don't know how to avoid that. I can do the check in that function but what do I need to do so the plugin is not activated? I have tried, only to see what could happened, to return false on that function but that do nothing. So what would I need to do?
To be honest, I've never tried to do what you're trying to do myself. Have you tried checking the plugin function files to see how activating/de-activating works?
This is simple template which you might need to use while activating / deactivating your plugin;
Activating;
function PLUGIN_NAME_activate()
{
// Your settings goes here
}
De-activating;
function PLUGIN_NAME_deactivate()
{
// settings deleting query goes here
}

You need to change PLUGIN_NAME with your plugin name.
Ok, I think I will better do that check somewhere else, and not the global deactivate or activate function of the plugin. I now have a switch on my plugin settings to turn it on/off. So what I would need to do is to check when the save button of my plugin settings form is clicked. That would be a way to validate my plugin settings form so they are not saved unless they are correct warning the user and staying on that settings page until he solves the offending setting or he lives the settings page navigating somewhere else.

Which is the proper hook? And how I identify I'm on the settings of my plugin in such a hook?
Yeah, he knows that yaldaram. He's asking for more than that.
In my opinion an on/off switch is redundant. It's the same as your activate/deactivate function. What exactly do you need check? If you want to check if a condition is met in order for your plugin to work, you could also use the pluginname_is_installed() function. If that function return false, the plugin can not be activated.
The condition to be met is a setting, indeed this has to be checked not only on activation when the plugin is installed (because the setting has not value yet -and besides it can't have a correct default, it is per user and does not depend on me. Imaging it like google analytics anyone has a key that would not depend on me if I code a plugin to deal with that-) but anytime the settings are changed, so I'm going to need to validate plugin settings anyway.

For its name I thought _is_installed() had to do with state installed/not installed, not with activate/deactivate and returning false on that function I thougth I would be in not installed state and that's not what I need. I need the plugin deactivated but with its settings accesible and I need the plugin never activated unless the settings are correct.
@OP:

Build your plugins this way:

Use these to add/remove your own settings and perform any database adjustments, such as adding a new field to an existing table, you can also add global templates here.
_install
_uninstall
_isinstalled

Use these to do any template edits, or add/remove per theme templates.
_activate
_deactivate

What this will do:
When you click Install & Activate it will run the install routine, thus performing all necessary setting additions to your database. It will then run the activate routine which will perform any template edits. If at this point you want the plugin to have no effect, but want to keep it installed so you can edit the settings and have them stay, then just deactivate it. When you deactivate it while using the proper install & uninstall routines, none of your database changes and settings will be removed. Only the template edits.


As for documentation:
http://wiki.mybb.com/index.php/Authoring_Plugins
http://wiki.mybb.com/index.php/Database_Methods
http://wiki.mybb.com/index.php/Plugin_Methods
http://wiki.mybb.com/index.php/MyBB_Plugin_Hooks
http://wiki.mybb.com/index.php/Development_Standards

The best way to learn how to create plugins, is to look at existing plugins and figure out how they work. There are things to avoid that you will see other plugin developers do, such as adding & removing settings inside the activate/deactivate functions. This is bad form, because then you need a "switch" as you described above to turn the plugin on/off w/out removing your settings. Not to mention all the extra (and thus unnecessary) code to make that switch work. Another thing that is worth looking at is the Akismet plugin included with MyBB. The template caching methodology at the top of that plugin is very useful if you have any custom templates that should get cached.
Pages: 1 2 3