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
mycred()

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!'
	);

}
mycred_get_users_cred()

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 );
mycred_get_users_fcred()

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 );
mycred_add()

Adjusts a users balance by adding a given amount of points to their balance with a log entry. Can not be used to deduct points as the amount you set will be converted into a positive value.

Available since version 1.0
mycred_add( 'reference', $user_id, 5, 'Points for being awesome!' );
mycred_subtract()

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 1.0
mycred_subtract( 'reference', $user_id, -5, 'Penalty!' );

Last edited September 2, 2016