Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Connecting WordPress Plugins with 3rd Party APIs Parsing JSON with PHP: Part 2

Lucas Santos
Lucas Santos
19,315 Points

Trying to add to my plugin but having trouble (based on the Wordpress Plugin Course)

So im trying to add a simple extra <li> under the "Points" & "Badges" that displays how many points you are away from 10000. The code works great but the problem here is that for some reason the until() function that I created doesn't seem to be getting the json $wptreehouse_profile->{'points'}->{'total'} value because the result is always 10000.

Here is the code from the course "wptreehouse_badges.php" with my added function at the bottom:

<?php

/*
 *  Plugin Name: Official Treehouse Badges Plugin
 *  Plugin URI: http://wptreehouse.com/wptreehouse-badges-plugin/
 *  Description: Provides both widgets and shortcodes to help you display your Treehouse profile badges on your website.  The official Treehouse badges plugin.
 *  Version: 1.0
 *  Author: Zac Gordon
 *  Author URI: http://wp.zacgordon.com
 *  License: GPL2
 *
*/

/*
 *  Assign global variables
 *
*/

$plugin_url = WP_PLUGIN_URL . '/wptreehouse-badges';
$options = array();
$display_json = true;
/*
 *  Add a link to our plugin in the admin menu
 *  under 'Settings > Treehouse Badges'
 *
*/

function wptreehouse_badges_menu() {

    /*
     *  Use the add_options_page function
     *  add_options_page( $page_title, $menu_title, $capability, $menu-slug, $function ) 
     *
    */

    add_options_page(
        'Official Treehouse Badges Plugin',
        'Treehouse Badges',
        'manage_options',
        'wptreehouse-badges',
        'wptreehouse_badges_options_page'
    );

}
add_action( 'admin_menu', 'wptreehouse_badges_menu' );


function wptreehouse_badges_options_page() {

    if( !current_user_can( 'manage_options' ) ) {

        wp_die( 'You do not have suggicient permissions to access this page.' );

    }

    global $plugin_url;
    global $options;
    global $display_json;

    if( isset( $_POST['wptreehouse_form_submitted'] ) ) {

        $hidden_field = esc_html( $_POST['wptreehouse_form_submitted'] );

        if( $hidden_field == 'Y' ) {

            $wptreehouse_username = esc_html( $_POST['wptreehouse_username'] );

            $wptreehouse_profile =  wptreehouse_badges_get_profile( $wptreehouse_username );

            $options['wptreehouse_username']    = $wptreehouse_username;
            $options['wptreehouse_profile']     = $wptreehouse_profile;
            $options['last_updated']            = time();

            update_option( 'wptreehouse_badges', $options );

        }

    }

    $options = get_option( 'wptreehouse_badges' );

    if( $options != '' ) {

        $wptreehouse_username = $options['wptreehouse_username'];
        $wptreehouse_profile =  $options['wptreehouse_profile'];

    }

    require( 'inc/options-page-wrapper.php' );


}

function wptreehouse_badges_get_profile( $wptreehouse_username ) {

    $json_feed_url = 'http://teamtreehouse.com/' . $wptreehouse_username . '.json';
    $args = array( 'timeout' => 120 );

    $json_feed = wp_remote_get( $json_feed_url, $args );

    $wptreehouse_profile = json_decode( $json_feed['body'] );

    return $wptreehouse_profile;

}

/*THIS IS THE FUNCTION THAT I CREATED TO GET YOUR POINTS - 1000*/
function until() {
                                    $master = 10000;
                                    $points = $wptreehouse_profile->{'points'}->{'total'};
                                    $till = $master - $points;
                                    if($points < $master) {
                                        echo "<li><strong>" . $till . "</strong> points until Master!</li>";
                                    }
}

function wptreehouse_badges_styles() {

    wp_enqueue_style( 'wptreehouse_badges_styles', plugins_url( 'wptreehouse-badges/wptreehouse-badges.css' ) );

}
add_action( 'admin_head', 'wptreehouse_badges_styles' );


?>

After this if you have followed the course I just added the until() function in the "options_page_wrapper.php" where we list the form.

<ul class="wptreehouse-badges-and-points">                          

                                    <li>Badges: <strong><?php echo count( $wptreehouse_profile->{'badges'} ); ?></strong></li>
                                    <li>Points: <strong><?php echo $wptreehouse_profile->{'points'}->{'total'}; ?></strong></li>
                                    <?php echo until(); ?>
                            </ul>

and like I said the code seems to work great and list the li within the function. Only thing is the result is always 10000 meaning my funtions didn't subtract my current points by 10000. Do not know why the $wptreehouse_profile; only works in "options_page_wrapper.php" meaning I can call $wptreehouse_profile{'profile'}->{'badges'}; in that file but not my main file "wptreehouse_badges.php" where my function and the main php code is. Please help.

1 Answer

Cody Michaels
PLUS
Cody Michaels
Courses Plus Student 27,272 Points

The issue you're having is with scope. Inside your function "$wptreehouse_profile->{'points'}->{'total'}" is null so when you subtract that from 10000 you get 10000.

One way to fix this is to pass $wptreehouse_profile->{'points'}->{'total'} to your function as an argument. I've pasted below what that would look like.

function until($points) {
    $master = 10000;
    $till = $master - $points;
    if($points < $master) {
        echo "<li><strong>" . $till . "</strong> points until Master!</li>";
    }
}
<ul class="wptreehouse-badges-and-points">                          
    <li>Badges: <strong><?php echo count( $wptreehouse_profile->{'badges'} ); ?></strong></li>
    <li>Points: <strong><?php echo $wptreehouse_profile->{'points'}->{'total'}; ?></strong></li>
    <?php echo until($wptreehouse_profile->{'points'}->{'total'}); ?>
</ul>
Henrik Hansen
Henrik Hansen
23,176 Points

And instead of echoing the html in the function, return the result and let the template decide what to output.