• Resolved Justin Scott

    (@justinscott)


    I’m a newbie when it comes to WordPress on the development, so when I look at the developer resources, I see stuff I want to use I just don’t know how. I’m looking for working examples as to how to code some of the stuff up.

    So I found this on the BadgeOS github page. Display point total function/shortcode I put it into one of my template pages and it works, but I would like to know why? Will I always “echo” a function in my custom template pages?

    one question answered brings several new questions.

    https://www.ads-software.com/plugins/badgeos/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hi Justin,

    Majority of “WordPress development” is going to be PHP development, and all of the fundamentals of PHP are going to apply.

    We’ll use the snippet you pointed out for a good example to start with.

    badgeos_get_users_points is one of the functions that was defined in the BadgeOS plugin as a “helper” function to achieve a bigger task. It’s also able to be used on its own.

    Inside the /badgeos/includes/points-functions.php file is the following:

    function badgeos_get_users_points( $user_id = 0 ) {
    
    	// Use current user's ID if none specified
    	if ( ! $user_id )
    		$user_id = wp_get_current_user()->ID;
    
    	// Return our user's points as an integer (sanely falls back to 0 if empty)
    	return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
    }

    It takes one parameter, $user_id, an integer, and that parameter defaults to 0. It will use the 0 value if no other integer value is passed in. The if ( ! $user_id ) line is a boolean check that if the $user_id has an integer value. In this case, the default 0 will be “false”. The exclamation mark is checking that a value of “false” is “true”. If that evaluates to “false is true”, it will grab the ID of the current user. Anything above 0 would be “true is true” and the $user_id = wp_get_current_user()->ID; line would be skipped.

    This allows you to use this function for whichever user may be viewing the page at the time, or a specific user.

    After that, it *returns* the absolute integer value of the user meta value stored for the specified user. Which, in this case, will be their total points.

    How the function returns data to the server is important. In this case, it returns the value, which allows you to use it in other code or assign to variables for further processing. To display it to the browser, you have to echo the value.

    If this line:
    return absint( get_user_meta( $user_id, '_badgeos_points', true ) );

    was

    echo absint( get_user_meta( $user_id, '_badgeos_points', true ) );,

    then all you’d have to do to use it is

    badgeos_get_users_points() or badgeos_get_users_points(4)

    Hopefully this is a good start.

    Function references:
    https://codex.www.ads-software.com/Function_Reference/absint

    Thread Starter Justin Scott

    (@justinscott)

    Thank you for the explanation on this function and how it works. I’ve grabbed a couple of PHP books and have been studying.

    as for the other functions that are available from the BadgeOS plugin, if I wanted to use them, I would just ” echo ” out the function in my theme template???

    I also have questions about some of the other developement docs badgeOS has. Should I just start a post for each one? It would be more like this post.

    Thanks Michael

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    It depends on if they return their value or echo them already. It’s really a function-by-function use case.

    Not sure how much value you’ll get from this right now, but we do have https://badgeos.org/api/ available. It’ll provide information on functions, parameters, etc. A bit more generic, but there is other developer docs available at https://badgeos.org/developers/

    Eh, you can just add your new questions to this one.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display point total function/shortcode’ is closed to new replies.