0

In the WordPress repo, on the main page for every plugin, it displays the date of the plugin's last update (screenshot). From my own WordPress site, is it possible to fetch this data with custom PHP and/or JavaScript code? I would like to fetch this information for each plugin that is installed on my site, then display it on the Dashboard-->Plugins page.

Perhaps there is some sort of API available which makes this data accessible for all plugins in the WordPress repo?

If possible, I might also want to fetch other data related to the plugin, e.g. 'WordPress version' and 'Tested up to.'

  • 1
    See this (and check the examples linked from there). – Sally CJ Aug 16 at 9:56
  • 1
    @SallyCJ Bringo, that's exactly what I wanted--thanks. Am I allowed to mark this as the solution? Or do you need to formally add it as an answer first? – cag8f Aug 17 at 14:22
  • I've just posted an answer.. :) – Sally CJ Aug 18 at 12:25
3

Perhaps there is some sort of API available

Yes, there is, and you can check it out here.

There are examples linked from that Codex page, and you might want to use version 1.2 (GET requests only) or 1.1 of the API where these versions both have the response format in JSON.

And actually, there's also plugins_api() which make things easy for you; however, you'd need to manually load the file where the function is defined (wp-admin/includes/plugin-install.php).

Example using plugins_api()

// You may comment this out IF you're sure the function exists.
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

$args = [
    'slug' => 'woocommerce',
];

$data = plugins_api( 'plugin_information', $args );
//var_dump( $data );

if ( $data && ! is_wp_error( $data ) ) {
    echo 'Latest version: ' . $data->version;
}

Example with manual HTTP requests

$args = [
    'slug' => 'woocommerce',
];

$url = 'http://api.wordpress.org/plugins/info/1.2/';
$url = add_query_arg( [
    'action'  => 'plugin_information', // first param for plugins_api()
    'request' => $args,                // second param for plugins_api()
], $url );

$res = wp_remote_get( $url );
if ( ! is_wp_error( $res ) ) {
    $data = json_decode( wp_remote_retrieve_body( $res ) );
    //var_dump( $data );
    echo 'Latest version: ' . $data->version;
}

Either way, if you want to exclude certain fields like reviews and read-me sections like "description" and "installation", you can use the fields argument like so:

$args = [
    'slug' => 'woocommerce',
    'fields' => [
        'sections' => false, // excludes all readme sections
        'reviews'  => false, // excludes all reviews
    ],
];
  • OK thanks for that. In the plugins_api() example you gave, you require()d plugin-install.php, with the caveat, // You may comment this out IF you're sure the function exists. Why wouldn't the function exist? If I was using a version of WordPress before 2.7? – cag8f Aug 20 at 14:22
  • Hi. Sorry, what I meant with that is, cases such as a plugin may have already included the file on the page. – Sally CJ Aug 21 at 6:19
  • 1
    OK got it--thanks very much! – cag8f Aug 21 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.