Last active
April 13, 2023 16:48
-
-
Save danieliser/0616eb81c6d375d19ca8 to your computer and use it in GitHub Desktop.
Plugins API Active Install Count Shortcode for WordPress
This file contains hidden or 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_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