Using custom code, how can I fetch data from the WordPress plugin repo?

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
    ],
];

Leave a Comment