WordPress.org

Support

Support » Plugins and Hacks » Total Download Count with WordPress Download Monitor

Total Download Count with WordPress Download Monitor

  • I’m using the below code in the functions.php file to retrieve the total downloads count for all files in the WordPress Download Monitor plugin and output the number as a shortcode [totaldwnlds].

    The code works perfectly fine but I’m sure this is probably not the best way to do it (and possibly vulnerable to attack?). Any suggested improvements?

    It would also be good to know how to output this number with comma separated values so it will display 3,234 instead of 3234.

    function dwnlds($atts, $content = null) {
        global $wpdb;
        $total_downloads = $wpdb->get_var("SELECT SUM(hits) FROM wp_download_monitor_stats ");
    return $total_downloads;
    }
    add_shortcode('totaldwnlds', 'dwnlds');

    UPDATE: ok fixed the number output format. The below code will display the number in the users local format:

    function dwnlds($atts, $content = null) {
        global $wpdb;
        $total_downloads = $wpdb->get_var("SELECT SUM(hits) FROM wp_download_monitor_stats ");
    return number_format_i18n($total_downloads, 0, '.', ',');
    }
    add_shortcode('totaldwnlds', 'dwnlds');
Viewing 1 replies (of 1 total)
  • UPDATE 2: This may be a more secure way of making the query using $wpdb->prepare function from http://codex.wordpress.org/Data_Validation#database

    function dwnlds($atts, $content = null) {
        global $wpdb;
        $total_downloads = $wpdb->get_var( $wpdb->prepare("SELECT SUM(hits) FROM wp_download_monitor_stats") );
    return number_format_i18n($total_downloads, 0, '.', ',');
    }
    add_shortcode('totaldwnlds', 'dwnlds');
Viewing 1 replies (of 1 total)
  • The topic ‘Total Download Count with WordPress Download Monitor’ is closed to new replies.