Reputation: 355
I am creating a new plugin. I want to run a php script at the time of plugin activation (only once, that script must never run again). For example register_activation_hook in wordpress. How can I do that? Please help me.
Upvotes: 0
Views: 481
Reputation: 169
There is no event that can let you run the script on plugin enabling. You could save in a separate file a flag that tells you if it is the first time the plugin is run.
Upvotes: 0
Reputation: 6470
Here is a link to Joomla Events Tutorial, has some useful stuff.
First of all you should use System plug-in, here is Joomla System Plug-in tutorial.
You need to identify the event at which you need to execute your script. I will use onAfterInitialise
which is the first system event.
Algo: create a plug-in with config parameter that will trigger the plug-in only once. You can also create a separate db table with configs for you param and query/update fields in that table instead. When plug-in is triggered for the first time execute custom code and set lock to prevent from executing in future..
Code:
<params>
<param name="executeOnInitialize" type="radio" default="1"
label="Execute on Init" description="Executes custom code on System onAfterInitialise">
<option value="0">No</option>
<option value="1">Yes</option>
</param>
</params>
function onAfterInitialise()
{
if ($this->params->get('executeOnInitialize', 0) == 1){
// TODO: execute custom code. You can package dirs with code into your plug-in install file. Then you can `require_once` files and use them.
// TODO: updated the executeOnInitialize = 1 in the plug-ins table
}
}
Upvotes: 2