如何以编程方式在我的wordpress博客上获得活动插件的列表?


13

我有2个博客,一个是多站点博客,一个不是。我想获得两个博客上都有效的插件列表,以便可以对其进行比较。在多站点博客上,我想列出在网络范围和站点范围内都启用的插件。

Answers:


20

激活的插件存储在WordPress博客的选项表中,在键下 active_plugins

因此您可以使用get_option('active_plugins'); 每个博客并比较数组。


2
值得补充的是,get_plugins()将为您提供所有插件,包括不活动的插件。
Charles Jaimet 2014年

13

以“仪表板”小部件的形式,一个用于“单个站点”和“网络站点”仪表板,另一个用于“多站点网络”仪表板。

/*
 * Single Site Dashboard Widget
 */
add_action('wp_dashboard_setup', 'wpse_54742_wp_dashboard_setup');

function wpse_54742_wp_dashboard_setup() {
    wp_add_dashboard_widget( 'wpse_54742_active_site_plugins', __( 'Active Plugins' ), 'wpse_54742_active_site_plugins' );
}

function wpse_54742_active_site_plugins() {
    $the_plugs = get_option('active_plugins'); 
    echo '<ul>';
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$value); // Folder name will be displayed
        echo '<li>'.$string[0] .'</li>';
    }
    echo '</ul>';
}


/*
 * Multisite Dashboard Widget
 */
add_action('wp_network_dashboard_setup', 'wpse_54742_network_dashboard_setup');

function wpse_54742_network_dashboard_setup() {
    wp_add_dashboard_widget( 'wpse_54742_active_network_plugins', __( 'Network Active Plugins' ), 'wpse_54742_active_network_plugins' );
}

function wpse_54742_active_network_plugins() {
    /*
     * Network Activated Plugins
     */
    $the_plugs = get_site_option('active_sitewide_plugins'); 
    echo '<h3>NETWORK ACTIVATED</h3><ul>';
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$key); // Folder name will be displayed
        echo '<li>'.$string[0] .'</li>';
    }
    echo '</ul>';


    /*
     * Iterate Through All Sites
     */
    global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("
        SELECT blog_id
        FROM {$wpdb->blogs}
        WHERE site_id = '{$wpdb->siteid}'
        AND spam = '0'
        AND deleted = '0'
        AND archived = '0'
    "));

    echo '<h3>ALL SITES</h3>';

    foreach ($blogs as $blog) {
        $the_plugs = get_blog_option($blog->blog_id, 'active_plugins'); 
        echo '<hr /><h4><strong>SITE</strong>: '. get_blog_option($blog->blog_id, 'blogname') .'</h4>';
        echo '<ul>';
        foreach($the_plugs as $key => $value) {
            $string = explode('/',$value); // Folder name will be displayed
            echo '<li>'.$string[0] .'</li>';
        }
        echo '</ul>';
    }
}

1
这远远超出了我的要求,但非常感谢您拨冗提供这么详细的答案。希望它将对其他人有所帮助。谢谢。
2012年

4

插件列表以及使用它们的网站(仅限多站点)

如果您想知道当前激活了哪个插件以及在哪个站点上,可以使用类似的功能:

function wpstars_list_active_plugins() {

  if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {

    echo "<table class='active-plugins'>";
    echo "<tr><th>Plugin name</th><th>Sites</th></tr>";

    $plugins = get_plugins();

    // Network activated
    $active_plugins = get_site_option('active_sitewide_plugins');
    foreach($active_plugins as $active_path => $active_plugin) {

      $plugins[$active_path]['Sites'] = "A,";
    }

    // Per site activated
    $sites = get_sites();
    foreach ( $sites as $site ) {

      $active_plugins = get_blog_option($site->blog_id, 'active_plugins');
      foreach($active_plugins as $active_plugin) {

        $plugins[$active_plugin]['Sites'] .= $site->blog_id . ",";
      }
    }

    foreach($plugins as $plugin) {

      echo "<tr><td>{$plugin['Name']}</td><td>{$plugin['Sites']}</td></tr>";
    }

    echo "</table>";
  }
}

1

WP-CLI只是票。我已经用了太多东西了,而我却数不清了!

wp plugin list --status=active

如果需要,可以使用别名在本地计算机上运行这些命令...

然后,您将使用@site函数

wp @all plugin list --status=active

要么

wp @multisite list --status=active
wp @blog list --status=active
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.