WordPress.org API – Get plugin authors favorite plugins

There have been some recent additions to the WordPress.org plugin repository. Most notably the changes to the plugin page and the author profile page which now shows an authors favorite plugins.

I want to create a sidebar widget plugin that shows a plugin authors favorites. I know how to use the API to get plugin stats and have also read DD32’s API Docs but I don’t believe docs exist on profiles or if a profile API even exists.

Read More

I attempted to use wp_remote_get and I am able to get the body html from the profile page but haven’t attempted to try and parse it yet as it seems a way to messy way of going about it. It would be great if I could get the profile in XML or json.

Are there any methods that I’m missing or does a profile API exist?

Edit:

Ok I have a beta version up on github using the SimpleHTML Dom parser. I don’t think I will be able to get the star ratings but I’m pretty happy with the results as a first go without an API.

WordPress.org does not allow content scraping and will ban you (via @otto). So this is a no go until a public API is released.

Related posts

Leave a Reply

2 comments

  1. Not yet.

    Otto said ‘soon’ on Wednesday. But he went to a BBQ this weekend, so ‘soon’ is probably ‘This month.’ 😉

    Edit:

    Otto42: @Ipstenu @EricMann I have code to do that, but not deployed yet. Some debate over the best way. It will be there eventually.

  2. The favorited plugins has been added to the WordPress.org API. There is a new feature in 3.5 that allows you to access your favorites from the plugin installer.

    See http://core.trac.wordpress.org/ticket/22002 for info on how it is being used in core.

    The API allows you to retrieve an object that contains contains each plugins

    • name
    • description
    • author
    • rating
    • last updated date
    • change log
    • stable version
    • works with wp version

    To retrieve the object

    Make a call to http://api.wordpress.org/plugins/info/1.0/ using wp_remote_post passing an array of arguments including the action which would be ‘query_plugins’ and the wp dot org username to retrieve the favorites from.

    $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) );
    

    Before you have a nice clean object you need to do some error handling and other parsing. Here is an example function that will return a nice clean object holding all the plugin details.

    function api( $action, $args ) {
            if ( is_array( $args ) )
                $args = (object) $args;
    
            $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) );
            if ( is_wp_error($request) ) {
                $res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
            } else {
                $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
                if ( ! is_object( $res ) && ! is_array( $res ) )
                    $res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
            }
    
            return apply_filters( 'c3m_favorite_results', $res, $action, $args );
        }
    

    Usage

    This example usage will give you an unordered list of favorite plugins along with a link to the plugin on dot org, a link to the author uri and the star rating.

    $api_data = api( 'query_plugins', array( 'user' => 'my_dot_org_username' ) );
    $api_plugins = $api_data->plugins;
    
    echo '<ul class="c3m-favorites">';
            foreach( $api_plugins as $plugin ) {
    
                $name = $plugin->name; ?>
                <li><strong><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $plugin->slug ?>/"><?php echo esc_html( $name ); ?></a></strong><br>
    
                    <div class="star-holder" title="<?php printf( _n( '(based on %s rating)', '(based on %s ratings)', $plugin->num_ratings ), number_format_i18n( $plugin->num_ratings ) ); ?>">
                    <div class="star star-rating" style="width: <?php echo esc_attr( str_replace( ',', '.', $plugin->rating ) ); ?>px"></div></div>
    
                    <em><?php _e('By: ') ?></em> <?php echo links_add_target( $plugin->author, '_blank' ). '<br>'; ?>
                </li><?php
            }
            echo '</ul>';
    

    Result

    enter image description here

    Widget screenshot from my Favorite Plugins Widget plugin:
    http://wordpress.org/extend/plugins/favorite-plugins-widget/