WordPress.org

Forums

[resolved] Using get_plugins() to pass plugin file path to is_active_plugin() (5 posts)

  1. Craig Ralston
    Member
    Posted 1 year ago #

    A rundown of my issue:
    I am working with a file that lives in the root along with wp-config.php and such. I am requiring wp-admin/includes/plugin.php and wp-load.php and my goal is to retrieve plugin data quickly and easily from a site. I have been mostly successful in my attempts, so far but have hit a small bump that I am hoping someone can assist with. My initial function uses the array get_plugins() and I am using data from that but I also need to check which plugins are active/inactive. Looking at the Codex, is_active_plugin($plugin) seems to do exactly what I want - Check if the plugin is active. I don't want to specify a file path, obviously, because I am checking all plugins, so I've been trying to use the data from get_plugins() to pass the file path to is_active_plugin but nothing has been working. If I do a var_dump(get_plugins();), the path I need is the key but if I try to use the key, the first plugin is just repeated every time. I suspect this is due to that fact that the get_plugins() array has another array for each plugin. Every plugin appears as inactive.

    function ws_plugin_list() {
        $all_plugins = get_plugins();
        echo '<ul style="list-style-type: none;">';
        foreach ($all_plugins as $plugin_item) { ?>
            <li>
                <a href="<?php echo $plugin_item['PluginURI']; ?>">
                         <?php echo $plugin_item['Title']; ?>
                </a>
            </li>
            <li>
                Version: <?php echo $plugin_item['Version']; ?><br />
                This plugin is currently <?php if (is_plugin_active(key($plugin_item))) { echo 'active'; } else { echo 'inactive'; } ?>
            </li>
            <br />
    <?php }
        echo '</ul>';
    }

    Here is the var_dump(get_plugins()); - As you can see, something like the akismet/akismet.php is what I am trying to collect and pass to is_plugin_active().

    array(3) { ["akismet/akismet.php"]=> array(11) { ["Name"]=> string(7) "Akismet" ["PluginURI"]=> string(31) "http://akismet.com/?return=true" ["Version"]=> string(5) "2.5.9" ["Description"]=> string(433) "Used by millions, Akismet is quite possibly the best way in the world to protect your blog from comment and trackback spam. It keeps your site protected from spam even while you sleep. To get started: 1) Click the "Activate" link to the left of this description, 2) Sign up for an Akismet API key, and 3) Go to your Akismet configuration page, and save your API key." ["Author"]=> string(10) "Automattic" ["AuthorURI"]=> string(40) "http://automattic.com/wordpress-plugins/" ["TextDomain"]=> string(0) "" ["DomainPath"]=> string(0) "" ["Network"]=> bool(false) ["Title"]=> string(7) "Akismet" ["AuthorName"]=> string(10) "Automattic" }

    I apologize if this is not the correct place to ask my question.

  2. iatp
    Member
    Posted 1 year ago #

  3. gmisura
    Member
    Posted 8 months ago #

    Craig,

    the "key()" function returns the item that is designated as the 'key' for the array. In this case, it is Name:

    error_log(' key(plugin_item] is ' . key($plugin_item));

    That returns:

    key(plugin_item] is Name

    You need the other foreach syntax:

    http://php.net/manual/en/control-structures.foreach.php

    foreach ( $all_plugins as $plugin_key => $plugin_item ) {

    Then you can use

    is_plugin_active($plugin_key) ? 'active' : 'inactive'

  4. Craig Ralston
    Member
    Posted 8 months ago #

    @gmisura - Yes, I actually figured this out many months ago - My apologies for leaving the thread unresolved and not providing the solution I went with.

    $all_plugins = get_plugins();
    $all_plugins_keys = array_keys($all_plugins);
    
    $loopCtr = 0;
    foreach ($all_plugins as $plugin_item) {
    
         // Get our Plugin data variables
         $plugin_root_file   = $all_plugins_keys[$loopCtr];
         $plugin_title       = $plugin_item['Title'];
         $plugin_version     = $plugin_item['Version'];
         $plugin_status      = is_plugin_active($plugin_root_file) ? 'active' : 'inactive';
    
    $loopCtr++;
    }
  5. Craig Ralston
    Member
    Posted 8 months ago #

    Resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic