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

Garrett Sanderson
Garrett Sanderson
12,735 Points

Error: Undefined index: wptreehouse_profile

I am getting an error notice. Not sure why this is happening. My code looks just like Zac's

Main Plugin File

<?php

/*
*
* Plugin Name: Official Treehouse Badges Plugin
* Plugin URI : http://wptreehouse.com/wptreehouse-badges-plugin/
* Description: Proviceds both widgets and shortcodes to help you display treehouse profile data
* Version 1.0
* Author: Garrett Sanderson
* Author URI: http://gsandersongraphics.com
* License: GPL2
*
*/


/*
 * Assign Global Variables
 *
*/

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

/*
 * Add a link to our plugin in the admin menu
 * under 'Settings > Treehouse Badges'
 *
*/

function wptreehouse_badges_menu() {
  /*
   * Use the add_option_page function
   * add_options ( $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 sufficient permissions to access this page');

  }

  global $plugin_url;
  global $options;

  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'];
  }

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



}

function wptreehouse_badges_get_profile ( $wptreehouse_username ) {

  $json_feed_url = 'http://teamtreeehouse,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' );

?>

My Settings Page

<h2><?php esc_attr_e( 'The Official Treehouse Badges Plugin', 'wp_admin_style' ); ?></h2>

<div class="wrap">

    <div id="icon-options-general" class="icon32"></div>
    <h1><?php esc_attr_e( 'Heading String', 'wp_admin_style' ); ?></h1>

    <div id="poststuff">

        <div id="post-body" class="metabox-holder columns-2">

            <!-- main content -->
            <div id="post-body-content">

                <div class="meta-box-sortables ui-sortable">

                    <?php if( !isset( $wptreehouse_username ) || $wptreehouse_username == '' ): ?>

                    <div class="postbox">

                        <div class="handlediv" title="Click to toggle"><br></div>
                        <!-- Toggle -->

                        <h2 class="hndle"><span><?php esc_attr_e( 'Lets Get Started', 'wp_admin_style' ); ?></span>
                        </h2>

                        <div class="inside">

              <form name="wptreehouse_username_form" method="post" action="">

                                <input type="hidden" name="wptreehouse_form_submitted" value="Y">

                <table class="form-table">
                  <tr>
                    <td><label for="wptreehouse_username">Treehouse Username</label></td>
                    <td><input type="text" name="wptreehouse_username" id="wptreehouse_username" class="regular-text" value="" /><br></td>
                  </tr>
                </table>
                <p>
                  <input class="button-primary" type="submit" name="treehouse_username_submit" value="Save" />
                </p>
              </form>
                        </div>
                        <!-- .inside -->

                    </div>
                    <!-- .postbox -->

                <?php else: ?>

                    <div class="postbox">

                        <div class="handlediv" title="Click to toggle"><br></div>
                        <!-- Toggle -->
                            <h3><span>Most Recent Badges</span></h3>
                        <div class="inside">
                            <p>Below are your 20 most recent badges</p>

                            <ul class="wp-treehouse-badges">
                                    <?php for( $i = 0; $i < 20; $i++ ): ?>
                                        <li>
                                            <ul>
                                                <li>
                                                    <img width="100%" class="wp-treehouse-gravatar" width="120px" src="<?php echo $plugin_url . '/images/wp-badge.png'; ?>">
                                                </li>
                                                <li class="wptreehouse-badge-name">
                                                    <a href="#">Badge Name</a>
                                                </li>
                                                <li class="wptreehouse-project-name">
                                                <a href="#">Project Name</a>
                                                </li>
                                            </ul>
                                        </li>
                                <?php endfor; ?>
                            </ul>

                        </div><!-- .inside -->

                    </div>
                    <!-- .postbox -->

                    <div class="postbox">

                        <div class="handlediv" title="Click to toggle"><br></div>
                        <!-- Toggle -->
                            <h3><span>Json Feed</span></h3>
                        <div class="inside">

                            <pre>
                                <code><?php var_dump( $wptreehouse_profile );?></code>
                            </pre>

                        </div>
                    </div>
                <?php endif; ?>


                </div>
                <!-- .meta-box-sortables .ui-sortable -->

            </div>
            <!-- post-body-content -->

            <!-- sidebar -->
            <div id="postbox-container-1" class="postbox-container">

                <div class="meta-box-sortables">

                    <?php if( isset( $wptreehouse_username ) && $wptreehouse_username != '' ): ?>

                    <div class="postbox">

                        <div class="handlediv" title="Click to toggle"><br></div>
                        <!-- Toggle -->

                        <h3><span>Mike the Frog's Profile</span></h3>

                        <div class="inside">
                            <p><img width="100%" class="wptreehouse-gravatar" src="<?php echo $plugin_url . '/images/mike-the-frog.png';?>"></p>

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

                                <li>Badges: <strong>200</strong></li>
                                <li>Points: <strong>10000</strong></li>

                            </ul>

                            <form name="wptreehouse_username_form" method="post" action="">
                                <p>
                                <input type="hidden" name="wptreehouse_form_submitted" value="Y">
                    <label for="wptreehouse_username">Username</label>
                    <input type="text" name="wptreehouse_username" id="wptreehouse_username" value="<?php echo $wptreehouse_username ?>" />
                  <input class="button-primary" type="submit" name="treehouse_username_submit" value="Update" />
                </p>
              </form>
                        </div>
                        <!-- .inside -->

                    </div>
                    <!-- .postbox -->
                <?php endif; ?>

                </div>
                <!-- .meta-box-sortables -->

            </div>
            <!-- #postbox-container-1 .postbox-container -->

        </div>
        <!-- #post-body .metabox-holder .columns-2 -->

        <br class="clear">
    </div>
    <!-- #poststuff -->

</div> <!-- .wrap -->

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

I think you commented out a line in the function:

<?php


function wptreehouse_badges_get_profile ( $wptreehouse_username ) {

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

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

// The line below was commented out
  $wptreehouse_profile = json_decode( $json_feed['body'] );

  return $wptreehouse_profile;

}