Skip to content

Instantly share code, notes, and snippets.

@lkoudal
Last active November 10, 2018 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkoudal/7a394e1f314687a6fedbd9a0e1d9b2af to your computer and use it in GitHub Desktop.
Save lkoudal/7a394e1f314687a6fedbd9a0e1d9b2af to your computer and use it in GitHub Desktop.
Get and display plugin information from wordpress.org repository via shortcode. Example [plugin_data id="delete-duplicate-posts"]
<?php
function plugin_data( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
if (!$a['id']) return '';
$response = return_plugin_data($a['id']);
if ($response) {
$result = '<div id="plugin-data-'.sanitize_html_class($response->name).'" class="plugin-data">';
$result .= '<div class="name">'.$response->name.'</div>';
$result .= '<div class="version">'.$response->version.'</div>';
$result .= '<div class="sections_description">'.$response->sections['description'].'</div>';
$result .= '<div class="downloaded">'.number_format_i18n($response->downloaded).'</div>';
$result .= '<div class="download_link"><a href="'.$response->download_link.'" target="_blank" class="et_pb_more_button et_pb_button et_pb_custom_button_icon">Download '.$response->name.'</a></div>';
$result .= '</div><!-- .plugin-data -->';
}
return $result;
}
add_shortcode( 'plugin_data', 'plugin_data' );
function return_plugin_data( $id ) {
$result = get_transient( 'plugin_data_' . $id );
if ( false === $result ) {
$args = (object) array( 'slug' => $id );
$request = array( 'action' => 'plugin_information', 'timeout' => 15, 'request' => serialize( $args) );
$url = 'http://api.wordpress.org/plugins/info/1.0/';
$response = wp_remote_post( $url, array( 'body' => $request ) );
$result = unserialize( $response['body'] );
set_transient( 'plugin_data_' . $id, $result, HOUR_IN_SECONDS );
}
return $result;
}
?>
@szmarinov
Copy link

Thank you! I was looking for such functionality. I'm trying to edit the code in order to use different info from the api in different such as just downloads, not whole info. For example: [plugin_data id="delete-duplicate-posts" "downloads"] is this possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment