• Resolved jennyong

    (@jennyong)


    Hi, can I know is there any hook to return level reached for badge? For example my badge have 4 level, and user reach level 3, so how to get level 3 via hook? Please advise. Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jennyong

    (@jennyong)

    Can I assign user role based on badge level?

    Hello @jennyong ,

    Hope you are doing well.

    To check the badge level that a user has reached and assign a user role based on the badge level, you can follow these steps:

    Get Badge Level:

    You can use the myCred get_users_badge function to retrieve the badge level for a specific user. Here’s an example of how you can use it:

    $user_id = get_current_user_id(); // Get the current user ID $badge_id = ‘badge_id’; // Replace ‘badge_id’ with the actual ID of your badge

    $badge_level = mycred_get_users_badge( $user_id, $badge_id );

    In this example, ‘$badge_level’ will contain the level of the specified badge for the current user.

    Assign User Role Based on Badge Level:

      You can use WordPress hooks like ‘user_register’ or ‘profile_update’ to check the badge level when a user registers or updates their profile. If the user has a specific badge level, you can assign a user role using the ‘wp_update_user’ function.

      Here’s an example:

      function assign_role_based_on_badge_level( $user_id ) {
          $badge_id = 'badge_id'; // Replace 'badge_id' with the actual ID or slug of your badge
          $target_level = 3; // Replace 3 with the desired badge level for the role assignment
      
      $badge_level = mycred_get_users_badge( $user_id, $badge_id );
      
      if ( $badge_level >= $target_level ) {
          $user = new WP_User( $user_id );
          $user->add_role( 'desired_role' ); // Replace 'desired_role' with the role you want to assign
      }
      }
      
      add_action( 'user_register', 'assign_role_based_on_badge_level' );
      add_action( 'profile_update', 'assign_role_based_on_badge_level' );

      In this example, the ‘assign_role_based_on_badge_level’ function checks if the user’s badge level is greater than or equal to the target level (3 in this case).
      If it is, the user is assigned the specified role.

      Let me know if you have any questions. We will help you out.

      Thanks & Regards,
      WP Experts Support Team

      Thread Starter jennyong

      (@jennyong)

      Hi @daniyalraza01

      Can provide documentation for mycred_get_users_badge? Because I just found mycred_get_users_badges function https://codex.mycred.me/functions/mycred_get_users_badges/.

      How can I get badge ID and display badge image if I create 1 badge only and have 3 levels under that badge? Which database table can I refer to get level id or user level?

      Thread Starter jennyong

      (@jennyong)

      Hi @daniyalraza01

      Is that possible for me to show current icon only using shortcode of [mycred_my_badges]? Or can advise how to show current icon only if 1 create 3 badges and I want to show current badge only.

      • This reply was modified 1 year, 5 months ago by jennyong.

      Hey Jennyong,

      This is the documentation link below.

      Documentation Link

      The shortcode ‘mycred_my_badges’ will display the current user earned badge level image. On the frontend you will see the Badge Level image which the user has earned. You will see the saved values in wp_postmeta table in badge_prefs meta key.

      Let me know if you have any confusion. We will help you out.

      Thanks

    Viewing 5 replies - 1 through 5 (of 5 total)
    • The topic ‘Badge Level’ is closed to new replies.