Playing with Balances
Accessing Balances
Since myCred stores balances as a custom user meta in the database, you can access a users balance using the get_user_meta function that WordPress provides.
With that being said, it’s important to remember that using the functions / classes myCred provides to access a balance, also adds support for some of the core myCred features, such as:
- Option to exclude certain users from having points.
- Multisite features.
You can get a users balance using the myCred_get_users_cred or myCred_get_users_fcred functions or by using the myCred Settings object via the myCred function. The balance will be returned by these functions either as an integer or as a float, based on the decimal places the point type in question is set to use.
Balance Related Functions
Function
Description
This function is the best way to gain access to multiple functions, all set to a particular point type. Ideal when you need to use two or more point related functions e.g. check to make sure a user is not excluded and then get their balance.
Available since version 1.4
$point_type = 'mytype';
$mycred = mycred( $point_type );
// Make sure user is not excluded
if ( ! $mycred->exclude_user( $user_id ) ) {
// get users balance
$balance = $mycred->get_users_balance( $user_id );
// adjust a users balance
$mycred->update_users_balance( $user_id, 5 );
// Adjust balance with a log entry
$mycred->add_creds(
'reference',
$user_id,
5,
'Points for being awesome!'
);
}
This function will return the given users balance for the given point type. The balance is returned as an integer or as a float. Note that as of version 1.7, this function will return boolean false if the user in question is excluded or if the user ID equals zero.
Available since version 1.0
$balance = mycred_get_users_cred( $user_id );
This function will return the given users balance for the given point type as a sting. The amount is formatted based on the point type’s setup. This includes (if used) a prefix, suffix, thousand separator and a decimal separator. Note that as of version 1.7, this function will return boolean false if the user in question is excluded or if the user ID equals zero.
Available since version 1.0
echo 'Your balance is: ' . mycred_get_users_fcred( $user_id );
Adjusts a users balance by adding a given amount of points to their balance with a log entry. Can be used to give or take points from the user by passing a positive or negative amount value.
Available since version 1.0
mycred_add( 'reference', $user_id, 5, 'Points for being awesome!' );
Adjusts a users balance by deducting a given amount of points from their balance with a log entry. Can not be used to give points to the user as the amount you set is converted into a negative value.
Available since version 10
mycred_subtract( 'reference', $user_id, -5, 'Penalty!' );