Table for WordPress Download Monitor

I was recently asked to provide a means to display the downloads managed by the WordPress Download Monitor (WDM) plugin in a traditional table format.  Here is the mockup of the desired format:

WDM by default keeps the Date, DL’s (hits), and DL Here link (url).  the POV, Opponent, Map and Event fields were added to each download as Custom Fields.

The design uses a shortcode function to assemble the table and insert it into the page.  Here is the function:

<?php
/***********   Shortcode for WordPress Download Monitor plugin table *****/
function mam_download_table_func($atts,$content='') {
   extract(shortcode_atts(array(
      'order' => 'DESC',
      'orderby' => 'date',
      'category' => '',
      'meta_name' => '',
      'vip' => 0,
      'rows_per_page' => '20',
   ), $atts));
   // Don't continue if WDM functions not found
   if (!function_exists('get_downloads')) {
      return "<h2>ERROR: Function get_downloads from WordPress Download Monitor plugin not found!</h2>";
   }
   /************  sample css  *******************/
      echo "<style type='text/css'>
#mam_downloads_wrapper { width: 100%; }
table#mam_downloads_table {
   border: 2px solid black;
   width: 100%;
}
table#mam_downloads_table thead {
   border-bottom: 4px double black;
}
table#mam_downloads_table tr th {
   border-left: 1px solid black;
   color: black;
   font-weight: bold;
   padding: 5px;
   text-align: center;
}
table#mam_downloads_table tr td {
   border: 1px solid #bbbbbb;
   padding: 5px;
}
</style>";

   $args = 'limit=0';
   if ($order) $args .= "&order=$order";
   if ($orderby) $args .= "&orderby=$orderby";
   if ($category) $args .= "&category=$category";
   if ($vip) $args .= "&vip=$vip";
   if ($meta_name) $args .= "&meta_name=$meta_name";
   $dls = get_downloads($args);
   if ($dls) {
      ob_start();
      echo "\n<div id='mam_downloads_wrapper'>\n";
      $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
      mam_dtf_paginate($current,$rows_per_page,sizeof($dls));
      mam_dtf_table_header();
      $start = ($current - 1) * $rows_per_page;
      $end = $start + $rows_per_page;
      $end = (sizeof($dls) < $end) ? sizeof($dls) : $end;
      // print_r("<h2>TESTING START: $start END: $end CURRENT: $current  ROWS PER PG: $rows_per_page</h2>");
      for ($i=$start;$i < $end ;++$i ) {
         $dl = $dls[$i];
         mam_dtf_output_row($dl);
      }
      mam_dtf_table_footer();
      mam_dtf_paginate($current,$rows_per_page,sizeof($dls));
      echo "\n</div><!-- End #mam_downloads_wrapper -->\n";
      $op = ob_get_clean();
      return $op;
   }
}
add_shortcode('mam_download_table','mam_download_table_func');
function mam_dtf_table_header() {
   // Output the table header
   echo "\n<div class='mam_downloads_table_wrapper'><table id='mam_downloads_table'><thead>\n";
   echo "<tr><th>Date</th><th>DL's</th><th>POV</th><th>vs.</th><th>Opponent</th><th>Map</th><th>Event</th><th>DL</th></tr>\n";
   echo "</thead>\n";
   echo "<tbody>\n";
}
function mam_dtf_table_footer() {
   // Output the table footer
   echo "</tbody></table></div><!-- End .mam_downloads_table_wrapper -->\n";
}
function mam_dtf_paginate($current,$rows_per_page=20,$total_rows) {
   // Output the pagination
   global $wp_rewrite;
   $pagination_args = array(
      'base' => @add_query_arg('paged','%#%'),
      'format' => '',
      'total' => ceil($total_rows/$rows_per_page),
      'current' => $current,
      'show_all' => false,
      'type' => 'plain',
   );
   if( $wp_rewrite->using_permalinks() )
       $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');
    if( !empty($wp_query->query_vars['s']) )
      $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    echo '<div>' . paginate_links($pagination_args) . '</div><!-- End .mam_downloads_pagination -->';
}
function mam_dtf_output_row($dl) {
   $meta = ($dl->meta) ? $dl->meta : array();
   $date = date("Y-m-d", strtotime($dl->date));
   echo "<tr>";
   echo "<td>$date</td>";
   echo "<td>$dl->hits</td>";
   echo "<td>{$meta['pov']}</td>";
   echo "<td>vs.</td>";
   echo "<td>{$meta['opponent']}</td>";
   echo "<td>{$meta['map']}</td>";
   echo "<td>{$meta['event']}</td>";
   echo "<td><a href='$dl->url' title='Downloaded $dl->hits times'>DL Here</a></td>";
   echo "</tr>\n";
}
/************  End of code for WordPress Download Monitor  ****************/
?>

Note that some sample CSS is included in the code – it really should be moved to your stylesheet.

The shortcode allows 6 parameters. The ‘order’, ‘orderby’, ‘category’, ‘meta_name’, and ‘vip’ parameters are simply passed through to the get_downloads() function of WDM. See its documentation for their usage.

The final parameter, ‘rows_per_page’, is used to specify the number of download rows to show on each page for pagination.

If you have any questions, or need help with this code, please post a Reply below or use the ‘Contact Me’ form.

4 Responses to Table for WordPress Download Monitor

  • anubhab says:

    I have implemented the table but i have added my new custom table name but i cannot insert any value to it .
    Plz help very urgent

  • Burim Avdiu says:

    Hello to you,
    I use these code in my buletin.php file but it not working for me.
    My code look like these,

    Place for your code.
    i just changed ‘category’ => ‘5’

    Can you help me how can i use these code.

    Best Regards, Burimi

    • Mac says:

      This code should be placed in your functions.php file or in the template for your Page. The Page should use the shortcode to insert the table. Is that what you did?

      Please use the Contact Me form to continue this conversation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*