The function below should do your job.
function manage_credits( $operation = 'add', $amount = 0 ) {
$user_id = get_current_user_id();
if ( $user_id ) {
$user_credit = (int) get_user_meta( $user_id, 'my_user_credit', true );
if ( 'add' == $operation ) {
$user_credit += $amount; //For adding credits
} elseif ( 'remove' == $operation && ( $user_credit >= $amount ) ) {
$user_credit -= $amount; //For removing credits
} elseif ( 'remove' == $operation ) {
echo 'You do not have enough credits to post.';
return;
}
update_user_meta( $user_id, 'my_user_credit', $user_credit );
}
}
Now all you need to do is while triggering add you will call manage_credits( 'add', 10 )
and while triggering remove you will call manage_credits( 'remove', 10 )
. The second parameter should be passed with the amount of credit you would like to add or remove.