WordPress插件错误:未定义索引:wptreehouse_profile

时间:2016-03-14 06:20:20

标签: php wordpress

我收到索引错误,我无法弄清楚为什么我会收到它。有人可以指出我正确的方向或解释这里发生了什么?

注意:未定义的索引:第82行的/Users/Garrett/Desktop/Sites/wordpressPractice.dev/wp-content/plugins/wptreehouse-badges/wptreehouse-badges.php中的wptreehouse_profile NULL

第82行

<?php $options['wptreehouse_profile']     = $wptreehouse_profile; ?>

主插件文件

<?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' );

    ?>

我的设置页面

<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 个答案:

答案 0 :(得分:1)

您正在尝试访问未设置的数组中的索引。这提出了一个通知。您收到此通知是因为您的php.ini已将 error_reporting 设置为包含 E_NOTICE

因此在PHP中,您必须在分配值之前检查变量是否已设置。 所以你的代码将如下所示,

if(isset($options['wptreehouse_profile'])) {
    $wptreehouse_profile  = $options['wptreehouse_profile'];
}

你将摆脱这种通知。