Hey oonskap.
My first thought was “Cant the myCRED Balance widget do this already?” and looking at the code, the answer is almost.
You see the myCRED Balance widget shows a users balance and optionally a message of your choosing for guests. However it does not accept any user related template tags in it’s current version. I feel however that allowing user related template tags in the “Layout:” filed for this widget is a good idea so I will include this in the next update.
But to get it to work before that, you could adjust the mycred-widgets.php file (located in mycred/includes/ folder) on line 56 from this:
$layout = $mycred->template_tags_amount( $instance['cred_format'], $balance );
to this:
$layout = $mycred->template_tags_amount( $instance['cred_format'], $balance );
global $wpdb;
$num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
$layout = str_replace( '%num_members%', $num_members, $layout );
$layout = $mycred->template_tags_user( $layout, $user_id );
Dont worry about making changes in the plugin files! I will implement this in the next version of myCRED.
This way, you could change the widgets “Layout:” filed to:
Welcome back %display_name%. You are 1 of %num_members% members in our community. You have %cred_f% Store Credit Points!
Notice that I created the %num_members% tempalate tag for you to use here. This template tag would only work in this particular widget filed.
Then you tick “Show message when not logged in”
and enter the message:
0 Store Credits. Register and get 100 Store Credits for free!
In case you do not want to use the widget I have included a function below that goes into your functions.php file and then called anywhere in your theme:
function my_custom_mycred_greet() {
if ( is_user_logged_in() && class_exists( 'myCRED_Core' ) ) {
global $current_user, $wpdb;
get_currentuserinfo();
// Load myCRED
$mycred = mycred_get_settings();
// Count members
$num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
// Welcome text
echo 'Welcome back ' . $current_user->user_login . '. You are 1 of <strong>' . $num_members . '</strong> members in our community. ';
echo 'You have ' . $mycred->get_users_cred( get_current_user_id() ) . ' Store Credits.';
}
else {
echo '0 Store Credits. Register and get 100 Store Credits for free!';
}
}
Alternatively, if you are using a prefix / suffix with your points:
function my_custom_mycred_greet() {
if ( is_user_logged_in() && class_exists( 'myCRED_Core' ) ) {
global $current_user, $wpdb;
get_currentuserinfo();
// Load myCRED
$mycred = mycred_get_settings();
// Balance
$balance = $mycred->get_users_cred( get_current_user_id() );
// Count members
$num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
// Welcome text
echo 'Welcome back ' . $current_user->user_login . '. You are 1 of <strong>' . $num_members . '</strong> members in our community. ';
// Balance formated with prefix/suffix
echo 'You have ' . $mycred->format_creds( $balance ) . ' Store Credits.';
}
else {
echo '0 Store Credits. Register and get 100 Store Credits for free!';
}
}
Then you just call this function anywhere in your theme:
<?php my_custom_mycred_greet(); ?>
There are 4 types of myCRED Template tags:
– Amount related
– User related
– Post related and
– Comment related
Template tags are used mainly for the log but also by myCRED widgets to help you customize layouts. It’s pretty easy to implement support for template tags in your code. The functions that handles template tags are located in the myCRED_Settings class and using the mycred_get_settings function you could access these methods directly.
Example:
$my_string = 'Hello %display_name%!';
$mycred = mycred_get_settings();
echo $mycred->template_tags_user( $my_string, get_current_user_id() );
There is one method for each template tag type and all of them require minimum two arguments. First the string that contains the template tags and second a reference id. For user related template tags the reference id is the users id, for posts it’s the post id, for comments it’s the comment id and for amounts it is the actual amount (unformatted).
Let me know if you need anything else.