Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active September 16, 2016 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabrielmerovingi/9534936 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/9534936 to your computer and use it in GitHub Desktop.
A simple shortcode that will return either the current users or a given users rank progress using Visual Composers Progress Bar.
/**
* Users Rank Progress
* This shortcode will render the users current rank progress using the Visual Composer Progress Bar shortcode.
* Shows either the current users rank progress or a given user ids. Returns the content if user is not logged in.
* Requries myCRED 1.4
* @version 1.1
*/
add_shortcode( 'mycred_rank_progress', 'mycred_pro_users_rank_progress' );
function mycred_pro_users_rank_progress( $atts, $content = NULL ) {
// Make sure we are logged in
if ( ! is_user_logged_in() ) return $content;
// Make sure myCRED is installed!
if ( ! function_exists( 'mycred' ) ) return '';
extract( shortcode_atts( array(
'user_id' => get_current_user_id()
) ) );
// Load myCRED
$mycred = mycred();
// Ranks are based on a total
if ( $mycred->rank['base'] == 'total' )
$key = $mycred->get_cred_id() . '_total';
// Ranks are based on current balance
else
$key = $mycred->get_cred_id();
// Get Balance
$users_balance = $mycred->get_users_cred( $user_id, $key );
// Rank Progress
// Get the users current rank post ID
$users_rank = (int) mycred_get_users_rank( $user_id, 'ID' );
// Get the ranks set max
$max = get_post_meta( $users_rank, 'mycred_rank_max', true );
// Calculate progress. We need a percentage with 1 decimal
$progress = number_format( ( ( $users_balance / $max ) * 100 ), 1 );
// Visual Composer customization
$options = '';
// If progress is less then 100%, use the stipped animation to indicate progress
if ( $progress < number_format( 100, 1 ) )
$options = 'options="striped,animated"';
// Else use the fixed background color to indicate completion
else
$progress = number_format( 100, 1 );
ob_start();
?>
<div class="bp-widget mycred-field">
<div class="wpb_row row">
<div class="col-md-12 col-sm-12 wpb_column column_container">
<div class="wpb_wrapper">
<div class="vc_text_separator wpb_content_element separator_align_left"><div>Rank Progress</div></div>
</div>
</div>
</div>
<?php echo do_shortcode( '[vc_row][vc_column width="1/1"][vc_progress_bar values="' . $progress . '|' . get_the_title( $users_rank ) . '" bgcolor="custom" ' . $options . ' custombgcolor="#E74C3C" title="" units="%"]<p><span class="description">your current progress towards the next rank</p>[/vc_column][/vc_row]' ); ?>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
@kocherm
Copy link

kocherm commented Aug 14, 2016

As of 1.7, this seems to be broken. It divides by zero on line 42

@kocherm
Copy link

kocherm commented Aug 14, 2016

Change line 36 to this:
$users_rank = (int) mycred_get_users_rank_id( $user_id, $type_id );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment