简体   繁体   中英

How can i run a php script on activating a plugin in joomla?

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.

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:

XML Param

<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>

PHP Plug-in Method

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
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM