Title: Create a function with the points variable
Last modified: August 21, 2016

---

# Create a function with the points variable

 *  Resolved [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/)
 * Hi, I wanna create sometthing like this:
 * if($points > 0 && $points < 100)
    return <span class=”level”>Level 1</span> else
   if($points > 100 && $points <250) return <span class=”level”>Level 2</span>
 * I dont know if this is correct because I am not good with php.
    But I think you
   can understand, I want to display a text based on the points value. Like the 
   example above, if the user has more than 0 points and less than 100 points it
   displays the user level.
 * Can you help me with this?
    (I am using it with buddypress) Thank you
 * [http://wordpress.org/extend/plugins/badgeos/](http://wordpress.org/extend/plugins/badgeos/)

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/page/2/?output_format=md)

 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894643)
 * Are those point ranges for badge points or for the user’s total points?
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894658)
 * user total points.
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894679)
 * Each user has a user meta key of “_badgeos_points” that stores their current 
   point total. Hopefully that helps a bit more.
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894680)
 * Okay, can you help me a little more with the code?
    By meta key of “_badgeos_points”
   what do you mean? I should use: if(_badgeos_points > 0 && _badgeos_points < 100)
   return <span class=”level”>Level 1</span> else if(_badgeos_points > 100 && _badgeos_points
   <250) return <span class=”level”>Level 2</span>
 * ?
    Thanks
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894682)
 * You’ll have to query for the user meta via get_user_meta() function. [Codex reference](http://codex.wordpress.org/Function_Reference/get_user_meta)
 * Basically assign that value to a variable like so
 *     ```
       $user_points = get_user_meta( 1, '_badgeos_points', true );
       if ( $user_points < '100' ) {
       	//Do something
       } else if ( $user_points >= '100' && $user_points < '250' )
       	//Do something
       } else {
       	//Sometihing default
       }
       ```
   
 * Can’t recall offhand exactly how well the string data will compare against each
   other, but I think what you see above should be a nice start.
 * Feel free to ask more questions if confused.
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894691)
 * Thank you for your answer, but I am having trouble to make this work!
    I used
   the function above and got a white screen in my profile :/
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894692)
 * could you paste what you tried exactly, so that I can help out. I did foolishly
   forget to say replace the first parameter with the actual user ID. If you need
   help retrieving that, I’ll have to type of a quick demo for that too.
 * Maybe it’d be best to ask your familiarity with php in general
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894693)
 * Well, my familiarity with php is small, the basics I think.
 * What I tried:
 *     ```
       <?php
       $user_points = get_user_meta( 1, '_badgeos_points', true );
       	if ( $user_points < '100' ) {
       	echo <span class="badge-points"><?php _e( "Level 1", 'viewr' ); ?></span>
       	} else if ( $user_points >= '100' && $user_points < '250' )
       	echo <span class="badge-points"><?php _e( "Level 2", 'viewr' ); ?></span>
       	} else {
       	echo <span class="badge-points"><?php _e( "Level 0", 'viewr' ); ?></span>
       	}
       ?>
       ```
   
 * For some reason this code isnt working, the php tag dont close properly.
    Also,
   if you can help me retrieving the current user id I will be grateful! And thanks
   again for helping me out!
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894698)
 * Yeah, that 1 in the get_user_meta corresponds to the current user ID, so you’ll
   need to actually change that part. Also as you noted, some php issues. Try this
   version below:
 *     ```
       <?php
       	//Grabs the current user for the viewing of the page.
       	$current_user = wp_get_current_user();
   
       	//grab the points user meta value, based on current user's ID
       	$user_points = get_user_meta( $current_user->ID, '_badgeos_points', true );
   
       	if ( $user_points < '100' ) { ?>
       		<span class="badge-points"><?php _e( "Level 1", 'viewr' ); ?></span>
       	<?php
       	} else if ( $user_points >= '100' && $user_points < '250' ) { ?>
       		<span class="badge-points"><?php _e( "Level 2", 'viewr' ); ?></span>
       	<?php
       	} else { ?>
       		<span class="badge-points"><?php _e( "Level 0", 'viewr' ); ?></span>
       	<?php
       	}
       ?>
       ```
   
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894700)
 * Thank you so much Michael! It worked perfectly!
    You are awesome man! 🙂
 * // Edit
    Another quick one, how can I make this code to work only if the badgeOS
   are present? Something like: <?php if (function_exists(‘badgeOS’)) { //the above
   code here; } else {} ?>
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894702)
 * Your example above looks like it should work. I’d just make sure to confirm that“
   badgeOS” is the actual function name, case sensitive.
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894703)
 * I only got it to work by checking if this function exists:
    badgeos_get_achievements
 *  [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894704)
 * Ah, if you were looking at this line:
 *     ```
       class BadgeOS
       ```
   
 * you’d need to use class_exists()
 * The plugin is using Object Orientated style of programming, at least for a decent
   chunk of it.
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894705)
 * Thank you very much for helping me with this!
    I made a plugin to display the
   points in the members header without need to touch the members header file 😀
 * Next Step is to make a plugin to show the user level!
 *  Thread Starter [rogerhnn](https://wordpress.org/support/users/rogerhnn/)
 * (@rogerhnn)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/#post-3894706)
 * Hi Michael, me again!
 * Can you please take a look at this code?
    [http://pastebin.com/Va8gqLjC](http://pastebin.com/Va8gqLjC)
 * Its the code that you helped me build, theres an error in some part, but as I
   said, I am not good with php.
 * Thanks again

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/page/2/?output_format=md)

The topic ‘Create a function with the points variable’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/badgeos_85d3bc.svg)
 * [BadgeOS](https://wordpress.org/plugins/badgeos/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/badgeos/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/badgeos/)
 * [Active Topics](https://wordpress.org/support/plugin/badgeos/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/badgeos/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/badgeos/reviews/)

 * 16 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/create-a-function-with-the-points-variable/page/2/#post-3894707)
 * Status: resolved