Advertisement
designbymerovingi

Custom Leaderboard

Jan 8th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. // Register shortcode
  2. add_action( 'init', 'register_my_shortcode' );
  3. function register_my_shortcode() {
  4.     add_shortcode( 'mycred_my_leaderboard', 'mycred_render_my_leaderboard' );
  5. }
  6.  
  7. // Shortcode render only if myCRED is installed
  8. if ( function_exists( 'mycred_label' ) ) :
  9.     function mycred_render_my_leaderboard( $atts, $content = NULL ) {
  10.         $attr = shortcode_atts( array(
  11.             'number'   => '-1',
  12.             'nothing'  => __( 'Leaderboard is empty.', 'mycred' )
  13.         ), $atts );
  14.        
  15.         // What user details to return besides the ID
  16.         $attr['user_fields'] = 'display_name,user_email';
  17.  
  18.         // Query the DB
  19.         $rankings = new myCRED_Query_Rankings( $attr );
  20.         $rankings->get_rankings();
  21.  
  22.         ob_start(); ?>
  23.  
  24. <table>
  25. <?php
  26.         // Have results
  27.         if ( $rankings->have_results() ) {
  28.  
  29.             // Loop though each row
  30.             foreach ( $rankings->result as $position => $user ) {
  31.                 // Result starts with zero
  32.                 $ranking = $position+1;
  33.            
  34.                 // Total
  35.                 $qty = get_user_meta( $user['ID'], 'qty', false );
  36.                 $total = array_sum( $qty );
  37.                
  38.                 // Product Names
  39.                 $products = get_user_meta( $user['ID'], 'product_name', false );
  40.                 $products = implode( '<br />', $products );
  41.                
  42.                 // Product IDs
  43.                 $product_ids = get_user_meta( $user['ID'], 'product_id', false );
  44.                
  45.                 // Product Images
  46.                 $images = array();
  47.                 if ( ! empty( $product_ids ) ) {
  48.                     foreach ( $product_ids as $pid ) {
  49.                         $image = wp_get_attachment_image_src( get_post_thumbnail_id( $pid ), 'single-post-thumbnail' );
  50.                         if ( $image !== false )
  51.                             $images[] = '<a href="' . get_permalink( $pid ) . '"><img src="' . $image[0] . '" width="100px" height="100px" /></a>';
  52.                     }
  53.                 } ?>
  54.  
  55.     <tr class="">
  56.         <td class="col-rank"><?php echo $ranking; ?></td>
  57.         <td class="col-pic">
  58.             <div class="whitebox pic-wrapper"><?php echo get_avatar( $user['user_email'], 96, home_url( '/wp-content/themes/Boke81/images/avatar.png' ) ); ?></div>
  59.         </td>
  60.         <td class="col-user">
  61.             <a data-role="hovercard" data-direction="right" data-user-id="<?php echo $user['ID']; ?>" class="name"><?php echo $user['display_name']; ?></a>
  62.         </td>
  63.         <td class="col-owns"><?php echo $total; ?></td>
  64.         <td class="col-img"><?php echo implode( '', $images ); ?></td>
  65.         <td class="col-points"><?php echo mycred_format_creds( $user['cred'] ); ?></td>
  66.     </tr>
  67. <?php
  68.             }
  69.            
  70.         }
  71.         // Else
  72.         else { ?>
  73.  
  74.     <tr>
  75.         <td colspan="6">No users found</td>
  76.     </tr>
  77. <?php
  78.         } ?>
  79.  
  80. </table>
  81. <?php
  82.         $output = ob_get_contents();
  83.         ob_end_clean();
  84.        
  85.         return $output;
  86.     }
  87. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement