Custom Leaderboard
-
Hey, great plugin and good to see somebody working on it after Cubepoints was abandoned.
I want to create a custom leaderboard page, maybe including other elements such as user roles and their avatars. Could you point me a simple way to build it with PHP, basically how to call rank, points, etc, using a page template?
-
Hey.
myCRED has the mycred_leaderboard shortcode which will show a leaderboard for your current users.
And here is an example of how to include a users avatar in the leaderboard:
add_filter( 'mycred_ranking_row', 'my_custom_ranking_rows', 10, 4 ); function my_custom_ranking_rows( $layout, $template, $row, $position ) { $avatar = get_avatar( $row['ID'], 32 ); return str_replace( '%avatar%', $avatar, $layout ); }You would then need to insert the %avatar% custom tag in your mycred_leaderboard template. You can use the mycred_ranking_row filter to add any type of custom template tags in your leaderboard.
Thanks, i got what i need this way:
$user = new WP_User( $row['ID'] ); if ( !empty( $user->roles ) && is_array( $user->roles ) ) { foreach ( $user->roles as $role ) $cargo = $role; } return str_replace( array ('%avatar%','%cargo%'),array($avatar, $cargo), $layout ); } add_filter( 'mycred_ranking_row', 'my_custom_ranking_rows', 10, 4 );Sorry, one more thing: when i use %user_profile_link%, it creates a link “mysite.com/author/Daniel Lemes”, and of course it doesn’t work. It should be “mysite.com/author/daniel-lemes”, using the “user_nicename”.
How to obtain it? I’ve tried this but with no results (blank link):
function my_custom_ranking_rows( $layout, $template, $row, $position ) { $avatar = get_avatar( $row['ID'], 32 ); $user_info = get_userdata($row['ID']); $nicename = $user_info->user_nicename; return str_replace( array ('%avatar%','%nicename%'), array( $avatar, $nicename ),$layout ); } add_filter( 'mycred_ranking_row', 'my_custom_ranking_rows', 10, 4 );… to create a template tag %nicename% and build my link “a href=”/author/%nicename%”
You can find all User related template tags in the myCRED Codex.
Very odd that you are getting an un encoded URL as the URL returned by that template tag is url encoded.
The %user_profile_link% template tag can return two different urls:
a) http://www.yourwebsite.com/ { author re-write base } / { user login encoded } /
b) BuddyPress profile URL.If you have a custom user profile URL setup you would need to create your own user template tag.
Appreciated, but none of this worked for me (i’ve tried to create a custom template tag %user_url%). The real odd is that only two first members on leaderboard had uncoded urls; others were fine.
I gave up and delete links from there. Thanks.
http://www.memoriabit.com.br/leaderboard/Could you paste in how you use the leaderboard shortcode in your page’s content field?
page-template-leaderboard: http://pastebin.com/VndMN4X1
functions.php: http://pastebin.com/TBJh7GFR
Your use of do_shortcode on line 5 in the functions.php file is incorrect. That shortcode will not return just the users rank but also several DIV elements!
To get a users rank, use the mycred_get_users_rank function. That way you get just the value and no html styling.
I would change line 5 to:
$seurank = mycred_get_users_rank( (int) $row['ID'] );It’s strange, but now the function to create my template tag is working and i could finally have proper links this way:
At first, on functions.php:
function my_custom_user_template_tags( $content, $user, $data ){ $user_info = get_userdata($user->ID); $content = str_replace( '%seu_nicename%', $user_info->user_nicename, $content ); return $content; } add_filter( 'mycred_parse_tags_user', 'my_custom_user_template_tags', 10, 3 );And on leaderboard template:
<a href="/author/%seu_nicename%">%display_name%</a>Thank you again!
Now i’m seeing error messages on MyCred Log, like this:
Warning: Missing argument 3 for my_custom_user_template_tags() in functions.php on line 1723
Line 1723 is:
function my_custom_user_template_tags( $content, $user, $data ){Try this:
function my_custom_user_template_tags( $content, $user, $data = NULL ) {If that does not work, then you could try adjusting your function:
function my_custom_user_template_tags( $content, $user, $data = NULL ) { // If $user is false, the entire user object is passed under $data if ( $user === false && is_object( $data ) ) $user_info = data; // else if see if the user id is in the $user object elseif ( isset( $user->ID ) ) $user_info = get_userdata( $user->ID ); // else see if the user id was passed on to $user elseif ( is_int( $user ) ) $user_info = get_userdata( $user ); else return $content; $content = str_replace( '%seu_nicename%', $user_info->user_nicename, $content ); return $content; }data = NULL worked fine, thank you!
The topic ‘Custom Leaderboard’ is closed to new replies.