Skip to content

Instantly share code, notes, and snippets.

@danieliser
Last active April 13, 2023 16:48
Show Gist options
  • Save danieliser/0616eb81c6d375d19ca8 to your computer and use it in GitHub Desktop.
Save danieliser/0616eb81c6d375d19ca8 to your computer and use it in GitHub Desktop.
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