How to add a new user badge after earning a certain number of user points? I'm using the User Badges and User Points modules. I want that when a user earns a certain number of user points, the user's old badge is replaced by a new one. For this, I have already created a rule with the actions "remove badge" and "add badge". The condition is "execute PHP code" and the code is:

$current_points = userpoints_get_current_points($userpoints_transaction-&gt;uid, 'all');
if ($current_points &gt;=7) {
  return true;
}


I also want to display the badge with the username at the top like StackExchange.

Note: Using the above code, it is displaying a message each time when user getting more than 7 user points. I want the message only once.
First of all, add the badge(s) you'd like to use at Configuration &gt; People &gt; Badges &gt; Add new badge. Afterwards, create a rule for every badge, similar to the following one:

{ "rules_new_badge" : {
    "LABEL" : "Add new badge",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules", "user_badges", "userpoints_rules" ],
    "ON" : { "userpoints_event_points_awarded_after" : [] },
    "IF" : [
      { "php_eval" : { "code" : "$current_points = userpoints_get_current_points($userpoints_transaction-\u003Euid, \u0027all\u0027);\r\nif ($current_points \u003E=7) {\r\n  return true;\r\n}" } }
    ],
    "DO" : [
      { "add_badge_by_name" : { "badge" : "1", "user" : [ "userpoints-transaction:user" ] } }
    ]
  }
}


Import it and modify it, according to your needs. To delete a badge, you can add another Rule with the action 'Remove badge by name'. Mind that I used a PHP Evaluation condition in order to use your PHP code in the Rule. I would, however, advise against this and use the Conditional Rules module instead.

Displaying the user badges next to the user names can be achieved by theming the comments in the comments.tpl.php file. This answer might help.