Posts: 1,247
Threads: 82
Joined: Feb 2011
2015-05-21, 04:20:21
Hello, I forgot where to find certain info regarding theme building.
What if you want to set certain code in your theme depending if a plugin is active, what code do you use?
Posts: 6,266
Threads: 181
Joined: Sep 2011
no function in 3.3.x
but you would do it like this.
PHP Code:
/**
* check if a plugin is active
* determine if a plugin is active
*
* @since 3.4
* @param string $pluginid
* @return bool returns true if active
*/
function pluginIsActive($pluginid){
GLOBAL $live_plugins;
return isset($live_plugins[$pluginid.'.php']) && ($live_plugins[$pluginid.'.php'] == 'true' || $live_plugins[$pluginid.'.php'] === true);
}
Posts: 1,247
Threads: 82
Joined: Feb 2011
(2015-05-21, 05:05:42)shawn_a Wrote: no function in 3.3.x
but you would do it like this.
PHP Code:
/**
* check if a plugin is active
* determine if a plugin is active
*
* @since 3.4
* @param string $pluginid
* @return bool returns true if active
*/
function pluginIsActive($pluginid){
GLOBAL $live_plugins;
return isset($live_plugins[$pluginid.'.php']) && ($live_plugins[$pluginid.'.php'] == 'true' || $live_plugins[$pluginid.'.php'] === true);
}
Ok, thanks. Although I don't really understand the code. It is for activating a plugins specific code in a theme, but only if plugin exist. I think I just make a separate template file.
Posts: 6,266
Threads: 181
Joined: Sep 2011
well no it just checks if a plugin is active
Posts: 305
Threads: 15
Joined: Mar 2014
To use shawn_a's function you should just do like this:
PHP Code:
<?php if (pluginIsActive('i18n_base')) { ?>
<div>You can only see me if i18n_base plugin is activated</div>
<?php } ?>
Or in a template file:
PHP Code:
<?php if (pluginIsActive('i18n_base')) { include('mytemplate.php'); } ?>
Posts: 1,247
Threads: 82
Joined: Feb 2011
2015-05-22, 08:32:37
(This post was last modified: 2015-05-22, 08:33:06 by datiswous.)
Thanks, but I want to do a certain thing instead of another (default) thing. So if plugin is active, do this, if not, do something else.
Posts: 6,266
Threads: 181
Joined: Sep 2011
The you just add an else{}
Posts: 1,247
Threads: 82
Joined: Feb 2011
Ok thanks, I see if I can make it work..
Posts: 3,491
Threads: 106
Joined: Mar 2010
Another way to know if a plugin is (or isn't) active is checking for some of the plugin's functions (that is available to the frontend), e.g.
PHP Code:
<?php if (!function_exists('i18n_init')) { ?>
<p>I18N plugin not activated!</p>
<?php } ?>
Posts: 6,266
Threads: 181
Joined: Sep 2011
Yeah like Carlos said, that's probably the best option if you are about to call the functions especially.