WordPress.org

Ready to get started?Download WordPress

Forums

Total Download Count with WordPress Download Monitor (2 posts)

  1. scottnz
    Member
    Posted 2 years ago #

    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');
  2. scottnz
    Member
    Posted 2 years ago #

    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');

Topic Closed

This topic has been closed to new replies.

About this Topic