I've created a couple of small plugins that I install on most of my websites. But new plugins are (fortunately) disabled by default when installed. I know it's only a couple of clicks to enable the plugin, but is it possible to make my plugin enable itself once the installation is done?
2 Answers
What I would recommend doing is creating an install script that executes a postflight
function. This function is built into Joomla and will get executed after the extension has installed.
You could use something like this:
function postflight($type, $parent)
{
// We only need to perform this if the extension is being installed, not updated
if (strtolower($type) === 'install')
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('enabled') . ' = 1',
$db->quoteName('ordering') . ' = 9999'
);
$conditions = array(
$db->quoteName('element') . ' = ' . $db->quote('plg_myplugin'),
$db->quoteName('type') . ' = ' . $db->quote('plugin')
);
$query->update($db->quoteName('#__extensions'))->set($fields)->where($conditions);
$db->setQuery($query);
$db->execute();
}
}
For more information on the script file, have a read of the Joomla Documentation:
http://docs.joomla.org/J2.5:Managing_Component_Updates_(Script.php)
Hope this helps
-
I believe that
1
and9999
are integers and there is no need for type casting ;) Commented Sep 27, 2014 at 8:26 -
@Farahmand - It can be used but I suppose not a major requirement. I actually asked a question about this a while back: joomla.stackexchange.com/questions/3738/… .. not sure why using type casting deserves a downvote though as the concept/approach is correct– LodderCommented Sep 27, 2014 at 9:56
You can create a plugin like this:
class plgExtensionEnableMyPlugins extends JPlugin
{
public function onExtensionAfterInstall($installer, $eid)
{
if ($eid)
{
$db = JFactory::getDbo();
$db->setQuery("UPDATE #__extensions SET `enabled` = 1 WHERE `extension_id` = $eid AND `type` = 'plugin'");
$db->execute();
}
}
}
Install and enable the above plugin before installing your plugins.
-
You are suggesting using an additional plugin to perform this action? So what if I'm a user downloading @johan's plugin and I expect it to auto publish? Will he expect me to install this plugin you mentioned before downloading his?– LodderCommented Sep 26, 2014 at 13:32
-
@Lodder, The question is about a "couple" of plugins that created by johan and he wants to install on "his" websites. If he had said the conditions you mentioned, my answer was something else ;) Commented Sep 27, 2014 at 8:21
-
@Farahmand, I do use the plugins mostly on sites I develop, but still, installing and enabling a plugin in order to auto-enable a couple of others seems a little overkill. But thanks for the code.– johanpwCommented Sep 27, 2014 at 10:40
-
@johanpw, No problem. Personally I prefer to use a plugin instead of to write
postflight
functions for all of my plugins. I believe that it's not an overkill. I can uninstall/disable the plugin as soon as I installed all of my plugins. Hope this helps people with similar case. Commented Sep 27, 2014 at 10:52