WordPress.org

Ready to get started?Download WordPress

Forums

function to determine if plugin activated? (3 posts)

  1. uudens
    Member
    Posted 5 years ago #

    is there a function to determine if a plugin is activated? something like is_plugin_activated($plugin_name)? is there any way to determine that from the plugin file itself? is there any way to determine that from "any" wp file?
    Thanks!

  2. Dwords
    Member
    Posted 5 years ago #

    Kind of depends on the plugin & what it's doing.

    This method I demonstrate here is not based on the plugin name, but based on the "function". A function is a little bit of PHP code, usually placed in your theme where you want the plugin's output to show up. So for example, this can work with a plugin that displays Recent Comments, or my example below is the TTF Title plugin where it displays the fancy title in colorful fonts. However this method would not really work with Akismet plugin or the Ozh Admin Drop-Down Menu, since that kind of plugin doesn't show something in your theme, but instead it just works in the background when activated. So this code is for plugins that use a function to display something on the public frontend of your site. You can wrap it with the PHP "if / then / else" code, to say: "If the function exists, then display the plugin's output using the function here. If the function does not exist then do __________ instead."

    Example, function is the_ttftitle

    Normally the plugin instructions would tell you to just put <?php the_ttftitle(); ?> in your theme. Or something with parameters like this <?php the_ttftitle($before="This is cool", $after="This is awesome"); ?>

    Read this carefully and you'll see where to move the function name, and where to move the parameters if you have parameters. Keep all the punctuation the same. This is my example of the code you are asking for:

    <?php if (function_exists('the_ttftitle')) { the_ttftitle($before="This is cool", $after="This is awesome"); } else { the_title(); }?>

    At the end, after the word "else" you can put anything inside { those brackets }. For example you could put a different plugin, a different function, or echo some text there such as "Sorry this plugin was not found".

    I recommend doing this for every single plugin that you put in your theme with a function. If you do not use the "if" code, you just put the plugin function, then your theme will break if the plugin is deactivated, making it impossible to check your blog with all-plugins-deactivated.

    Plugin authors should give out the code to display their function pre-written in this format, because it is safer for your theme & most novice WP users do not know how to write that code. It took me a year to figure that out, once I did I went back and put it on all my plugin functions.

  3. uudens
    Member
    Posted 5 years ago #

    thanks, so i assume there is no direct way of testing it through wordpress, at least an easy one. but your solution should work just the way i need it and its easy enough. thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic