Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plugins API Active Install Count Shortcode for WordPress
<?php
function plugin_install_count_shortcode( $atts ) {
$a = shortcode_atts( array(
'plugin' => NULL,
), $atts );
if( ! $a['plugin'] ) {
return;
}
if( ( $count = get_transient( 'plugin_install_count-' . $a['plugin'] ) ) ) {
return $count;
}
if( ! function_exists( 'plugins_api' ) ) {
include_once ABSPATH . '/wp-admin/includes/plugin-install.php';
}
$api = plugins_api( 'plugin_information', array(
'slug' => $a['plugin'],
'fields' => array( 'active_installs' => true )
) );
if( is_wp_error( $api ) ) {
return;
}
set_transient( 'plugin_install_count-' . $a['plugin'], $api->active_installs, 24 * HOUR_IN_SECONDS );
return $api->active_installs;
}
add_shortcode( 'plugin_install_count', 'plugin_install_count_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment