最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How get list of updates of wp site?

programmeradmin0浏览0评论

i need to get a list of all updates the wp site need (core, plugin and themes). i already know about wp_get_update_data(), but it give me back only the number. i need to get for example:

plugin1 | actual version | new version

plugin2 | actual version | new version

theme1 | actual version | new version

wp | actual version | new version

how can i save that information into some arrays to use them?

edit: i managed to do this with what i found but it doesn't work, it print to try only test

function data_report()
{
    // Get theme info
    $theme_data = wp_get_theme();
    $theme = $theme_data->Name . ' ' . $theme_data->Version;

    // Get plugins that have an update
    $updates = get_plugin_updates();

    // WordPress active plugins
    $fin_plug = "\n" . '-- WordPress Active Plugins' . "\n\n";
    $plugins = get_plugins();
    $active_plugins = get_option('active_plugins', array());
    foreach ($plugins as $plugin_path => $plugin) {
        if (!in_array($plugin_path, $active_plugins)) {
            continue;
        }
        $update = array_key_exists($plugin_path, $updates) ? ' (needs update - ' . $updates[$plugin_path]->update->new_version . ')' : '';
        $fin_plug .= $plugin['Name'] . ': ' . $plugin['Version'] . $update . "\n";
    }

}


add_action( 'wp_footer', 'stampa_prova' );

function stampa_prova (){   
        echo 'test';
        $cospi = data_report();
        echo $cospi;
}

i need to get a list of all updates the wp site need (core, plugin and themes). i already know about wp_get_update_data(), but it give me back only the number. i need to get for example:

plugin1 | actual version | new version

plugin2 | actual version | new version

theme1 | actual version | new version

wp | actual version | new version

how can i save that information into some arrays to use them?

edit: i managed to do this with what i found but it doesn't work, it print to try only test

function data_report()
{
    // Get theme info
    $theme_data = wp_get_theme();
    $theme = $theme_data->Name . ' ' . $theme_data->Version;

    // Get plugins that have an update
    $updates = get_plugin_updates();

    // WordPress active plugins
    $fin_plug = "\n" . '-- WordPress Active Plugins' . "\n\n";
    $plugins = get_plugins();
    $active_plugins = get_option('active_plugins', array());
    foreach ($plugins as $plugin_path => $plugin) {
        if (!in_array($plugin_path, $active_plugins)) {
            continue;
        }
        $update = array_key_exists($plugin_path, $updates) ? ' (needs update - ' . $updates[$plugin_path]->update->new_version . ')' : '';
        $fin_plug .= $plugin['Name'] . ': ' . $plugin['Version'] . $update . "\n";
    }

}


add_action( 'wp_footer', 'stampa_prova' );

function stampa_prova (){   
        echo 'test';
        $cospi = data_report();
        echo $cospi;
}
Share Improve this question edited Feb 6, 2020 at 13:31 DevBeppe asked Feb 6, 2020 at 10:56 DevBeppeDevBeppe 477 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4

When on the admin view you can use the following functions to get update data.

  • get_plugin_updates()
  • get_theme_updates()
  • get_core_updates()

If you want to build your own functions, then you can get update related data from site transients update_themes, update_plugins, and update_core. Then you can compare the plugin and theme transients with the theme and plugin arrays you can get with wp_get_themes() and get_plugins(). The above functions are more or less wrappers for these transients and functions.

Have a look at the source for the functions to see how WP handles this stuff.

The answer made by Antti Koskinen is great. It helped me to make a function to get the plugins info for a custom API endpoint, I hope it can help somebody else

    function system_check() {
        if (!function_exists('get_plugins')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        if (!function_exists('get_site_transient')) {
            require_once ABSPATH . 'wp-admin/includes/option.php';
        }
        $updates = get_site_transient('update_plugins');
        $plugins = get_plugins();
        $the_list = array();
        $i = 1;
        if ($updates->response) {
            $the_list["ultima_revision"] = date("Y-m-d g:i A", intval($updates->last_checked));
            foreach ($plugins as $name => $plugin) {
                $the_list["plugins"][$i]["id"] = $name;
                $the_list["plugins"][$i]["name"] = $plugin["Name"];
                $the_list["plugins"][$i]["current_version"] = $plugin["Version"];
                if (isset($updates->response[$name])) {
                    $the_list["plugins"][$i]["update"] = "yes";
                    $the_list["plugins"][$i]["version"] = $updates->response[$name]->new_version;
                } else {
                    $the_list["plugins"][$i]["update"] = "no";
                }
                $i++;
            }
        }
        return $the_list;
    }

solved, if someone need the code i posted work, need only to add the admin permission to execute with require_once

发布评论

评论列表(0)

  1. 暂无评论