Skip to content

Instantly share code, notes, and snippets.

@lkoudal lkoudal/functions.php Secret
Last active Nov 10, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Nov 10, 2018

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
You can’t perform that action at this time.