Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
myCRED Custom Shortcode: Rank Progress
<?php
// Get user's rank progress
function get_mycred_users_rank_progress( $user_id, $show_rank ) {
global $wpdb;
if ( ! function_exists( 'mycred' ) ) return '';
// Change rank data to displayed user when on a user's profile
if ( function_exists( 'bp_is_user' ) && bp_is_user() && empty( $user_id ) ) {
$user_id = bp_displayed_user_id();
}
// Load myCRED
$mycred = mycred();
// Check if user is excluded
if ( $mycred->core->exclude_user( $user_id ) ) return '';
// 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 name of the users current rank
$users_rank_name = get_the_title( $users_rank );
// Get the ranks set max
$max = get_post_meta( $users_rank, 'mycred_rank_max', true );
$tabl_name = $wpdb->prefix . 'postmeta';
// Get the users next rank post ID
$next_ranks = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$tabl_name} WHERE meta_key = %s AND meta_value > %d ORDER BY meta_value * 1 LIMIT 1;", 'mycred_rank_min', $max ) );
foreach( $next_ranks as $next_rank ) {
$next_rank = $next_rank->post_id;
}
// Get the name of the users next rank
$next_rank_name = get_the_title( $next_rank );
// Get the ranks set min
$next_rank_min = get_post_meta( $next_rank, 'mycred_rank_min', true );
// Calculate progress. We need a percentage with 1 decimal
$progress = number_format( ( ( $users_balance / $max ) * 100 ), 0 );
// Display rank progress bar
echo '<div class="mycred-rank-progress">';
echo '<h3 class="rank-progress-label" style="font-weight:bold;">Rank Progress ('. $progress .'%)</h3>';
echo '<progress max="'. $max .'" value="'. $users_balance .'" class="rank-progress-bar">';
echo '</progress>';
if( $show_rank == 'yes' ){
echo '<span class="current-rank" style="float:left;padding-top:1%;font-weight:bold;">'. $users_rank_name .'</span>';
echo '<span class="next-rank" style="float:right;padding-top:1%;font-weight:bold;">'. $next_rank_name .'</span>';
echo '<span class="points-progress" style="width:100%;float:left;margin-top: -4.5%;padding-top:1%;font-weight:bold;text-align:center;">'. $users_balance .' of '. $next_rank_min .'</span>';
}
echo '</div>';
}
/**
* myCRED Shortcode: mycred_users_rank_progress
* @since 1.0
* @version 1.0
*/
function mycred_users_rank_progress( $atts ){
extract( shortcode_atts( array(
'user_id' => get_current_user_id(),
'show_rank' => 'no'
), $atts ) );
ob_start();
get_mycred_users_rank_progress( $user_id, $show_rank );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'mycred_users_rank_progress', 'mycred_users_rank_progress' );
?>
.mycred-rank-progress{}
.mycred-rank-progress h3.rank-progress-label{}
.mycred-rank-progress span.current-rank{}
.mycred-rank-progress span.next-rank{}
.mycred-rank-progress span.points-progress{}
/* HTML 5 Progress Bar */
progress[value] {
/* Reset the default appearance */
-webkit-appearance: none; -moz-appearance: none; appearance: none;
/* Get rid of default border in Firefox. */
border: none;
/* Dimensions */
width: 100%; height: 2em;
/* For IE10 */
color: red;
}
/* HTML 5 Progress Bar Container */
progress[value]::-webkit-progress-bar {
background-color: #eee;
}
/* HTML 5 Progress Bar Value */
progress[value]::-webkit-progress-value {
background-color: red;
}
@ltsharma

This comment has been minimized.

Copy link

commented Apr 19, 2015

if ( $mycred->core->exclude_user( $user_id ) ) return ''; is error, removing this line works perfectly fine

@Basti24

This comment has been minimized.

Copy link

commented Sep 6, 2016

After updating myCred from 1.6.9 to 1.7.2 (https://mycred.me/support/changelog/) some things stopped working using this code snippet with [mycred_users_rank_progress show_rank="yes"]

  • precentage always zero / 0%
  • progress bar is always completed/ full
  • rank base titel isn't shown anymore (placed with random text from page)

Can someone help me out or have a fix?
Thx!

code used

`function get_mycred_users_rank_progress( $user_id, $show_rank ) {
global $wpdb;

if ( ! function_exists( 'mycred' ) ) return '';

// Change rank data to displayed user when on a user's profile
if ( function_exists( 'bp_is_user' ) && bp_is_user() && empty( $user_id ) ) {
    $user_id = bp_displayed_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 name of the users current rank
$users_rank_name = get_the_title( $users_rank );

// Get the ranks set max
$max = get_post_meta( $users_rank, 'mycred_rank_max', true );

$tabl_name = $wpdb->prefix . 'postmeta';

// Get the users next rank post ID
$next_ranks = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$tabl_name} WHERE meta_key = %s AND meta_value > %d ORDER BY meta_value * 1 LIMIT 1;", 'mycred_rank_min', $max ) );

foreach( $next_ranks as $next_rank ) {

    $next_rank = $next_rank->post_id;
}

// Get the name of the users next rank
$next_rank_name = get_the_title( $next_rank );

// Get the ranks set min
$next_rank_min = get_post_meta( $next_rank, 'mycred_rank_min', true );

// Calculate progress. We need a percentage with 1 decimal
$progress = number_format( ( ( $users_balance / $max ) * 100 ), 0 );

// Display rank progress bar
echo '<div class="mycred-rank-progress">';  
    echo '<h3 class="rank-progress-label" style="font-weight:bold;padding-bottom:1%;">Ihr Fortschritt ('. $progress .'%)</h3>';
    echo 'bis zum nächsten Rang </br>';
    echo '<progress max="'. $max .'" value="'. $users_balance .'" class="rank-progress-bar">';
    echo '</progress>'; 
    if( $show_rank == 'yes' ){
        echo '<span class="current-rank" style="float:left;padding-top:1%;">'. $users_rank_name .'</span>'; 
        echo '<span class="next-rank" style="float:right;padding-top:1%;">'. $next_rank_name .'</span>';
        echo '<span class="points-progress" style="width:100%;float:left;margin-top: -3.5%;padding-top:1%;text-align:center;">'. $users_balance .' von '. $next_rank_min .'</span>';
    }
echo '</div>';  

}`

`function mycred_users_rank_progress( $atts ){
extract( shortcode_atts( array(
'user_id' => get_current_user_id(),
'show_rank' => 'no'
), $atts ) );

ob_start();

get_mycred_users_rank_progress( $user_id, $show_rank );

$output = ob_get_contents();
ob_end_clean();

return $output;

}
add_shortcode( 'mycred_users_rank_progress', 'mycred_users_rank_progress' );`

@AlexanderCnb

This comment has been minimized.

Copy link

commented Sep 19, 2016

  • Find:
    // Get the users current rank post ID
    $users_rank = (int) mycred_get_users_rank( $user_id, 'ID' );
  • Replace with:
    // Get the users current rank post ID
    $users_rank = mycred_get_users_rank_id( $user_id, $type_id );

v1.7 updated rank functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.