Using a shortcode to fetch WordPress plug-in information

I may have a low threshold for managing information, but I find that with seven plug-ins in the WordPress repository it’s a struggle to keep my local information on those plug-ins always up to date. I have a page here about each plug-in, and it’s not uncommon that I’ll publish an update and go to check my local information just to find that my own page on that plug-in is six months out of date…

One way to keep that information nicely in sync is to actually import it directly from the WordPress plug-in repository’s information API. The API documentation is available, if you want to roll your own, but here’s a very basic bit of code to grab it yourself.

Creating a shortcode in WordPress is crazy-easy:


add_shortcode( 'plugin', 'get_plugin' );
function get_plugin( $atts ) {
    extract(shortcode_atts(array(
        'slug'=>''
      ), $atts));
    if ( $slug ) return show_plugin_info( $slug );
}

Woo hoo! You have a shortcode. This shortcode takes one attribute, “slug”, which will be the plug-in slug as seen in the repository. Now, technically speaking, what you have right now is a fatal error, because we haven’t actually defined the ‘show_plugin_info’ function. Fair enough — let’s do that.


function show_plugin_info( $slug ) {
    $body = (object) array( 'slug' => $slug );
    $post_data = array('action' => 'plugin_information', 'request' => serialize($body));
    $return = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $post_data ) );
    $plugin = unserialize($return['body']);	
    $return = "
            <p>Current Version: $plugin->version<br />
            Requires: $plugin->requires<br />
            Tested to: $plugin->tested<br />
            Downloads: ".number_format($plugin->downloaded)."<br />
            Last updated: $plugin->last_updated</p>";
    return $return . wpautop($plugin->sections['description']);
}

This is a pretty simple bit of scripting -- and, practically speaking, it's customized to show what I actually do on my own site. You may not want to even use this much code!

I'm using the WordPress wrapper for http requests to get the data -- this is much more reliable than using something like cURL or fopen, because the wrapper will query whatever transport is actually available, rather than depending on the specific wrapper you've defined.

The API is looking for very simple information -- just the slug for the plug-in, and the knowledge that we're accessing the plugin_information API.

With that, we retrieve an object containing all the basic information for the plug-in, largely drawn from the plug-in's readme.txt file. I've only chosen to incorporate a few fields into my site - but you can also grab several other fields this way. These fields are detailed at Dion's API information site, so I won't repeat them here.

This is a very basic shortcode -- simply place [plugin slug='wp-accessibility'] in your post or page and you'll get all the information about the plug-in you referenced.

Not sure how many other people are likely to find this useful...but for my purposes, it's pretty handy.

Have something to contribute?




« Read my Comment Policy

Start the conversation!

Return to Top