-
-
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"]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?