Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress How to Build a WordPress Plugin Connecting WordPress Plugins with 3rd Party APIs Getting and Storing a JSON Feed

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,845 Points

Can't seem to get passed NULL on the screen. Not getting any JSON

<?php
/*
Plugin Name: Treehouse Badges Plugin
Plugin URI:  http://www.teamtreehouse.com
Description: Grabs Treehouse badges from user profile and displays on website.
Version:     1
Author:      Zac Gordon via Treehouse Course
Author URI:  http://www.twitter.com/zgordon
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: treehouse-badge
*/

/*
 Add URL to plugin  - Assign global variabls
*/

$plugin_url = WP_PLUGIN_URL . '/wptreehouse-badges';
$options = array();



function wptreehouse_badges_menu() {


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

}

add_action('admin_menu', 'wptreehouse_badges_menu');


/* The options page function */

function wptreehouse_badges_options_page() {

    if(!current_user_can ('manage_options') ) {
      wp_die('Sorry, you don\'t have the proper permissions!');
    }

    global $plugin_url;
    global $options;
    if(isset ($_POST['wptreehouse_form_submitted'] ) ) {

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

        $wptreehouse_profile = wptreehouse_badges_get_profile ( $wptreehouse_username );


        if($hidden_field == 'Y') {

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

            $options['wptreehouse_username'] = $wptreehouse_username;
            $options['wptreehouse_profile']  = $treehouse_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'];
    }

    var_dump($wptreehouse_profile);

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



function wptreehouse_badges_get_profile( $wptreehouse_username ) {  

    $json_feed_url = 'http://www.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;

}



function wptreehouse_badges_styles() {

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

add_action('admin_head','wptreehouse_badges_styles');
?>

I have a problem with my Treehouse plugin in that I'm not getting any JSON returned. I keep getting NULL on the screen despite following the video. Don't know what I'm doing wrong.

I'm working on localhost.

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

I believe you named a variable wrong.

<?php


//Here you store the Json in a var.
$wptreehouse_profile = wptreehouse_badges_get_profile ( $wptreehouse_username );


        if($hidden_field == 'Y') {

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

            $options['wptreehouse_username'] = $wptreehouse_username;

           // and here you use another var to store in Db, 'wp' is missing in the var name.
            $options['wptreehouse_profile']  = $treehouse_profile; 
            $options['last_updated']         = time();

            update_option('wptreehouse_badges', $options);
        }

?>