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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Trouble adding a Wordpress Widget: re: Build a Plugin Course

But I think I'm getting the theory nailed down with Wordpress Widgets, indeed i was able to complete the code challenge to do that.

But I was trying to do it on the example site I had which is in localhost. There's a space for the widget in my admin area but no title and these 2 error messages appear at the top.

Warning: Missing argument 1 for WP_Widget::__construct(), called in C:\xampp\htdocs\wp-plugin\wp-includes\class-wp-widget-factory.php on line 43 and defined in C:\xampp\htdocs\wp-plugin\wp-includes\class-wp-widget.php on line 154

Warning: Missing argument 2 for WP_Widget::__construct(), called in C:\xampp\htdocs\wp-plugin\wp-includes\class-wp-widget-factory.php on line 43 and defined in C:\xampp\htdocs\wp-plugin\wp-includes\class-wp-widget.php on line 154

Here is my code

wp-treehousebadges.php

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

class Wptreehouse_Badges_Widget extends WP_Widget {

    function wptreehouse_badges_register_widgets() {
        // Instantiate the parent object
        parent::__construct( false, 'Official Treehouse Badges Wordpress Plugin' );
    }

    function widget( $args, $instance ) {
        // Widget output

        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $options = get_option('wptreehouse_badges');
        $wptreehouse_profile = $options['wptreehouse_profile'];

        require('inc/front-end.php');
    }

    function update( $new_instance, $old_instance ) {
        // Save widget options

        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);

        return $instance;
    }

    function form( $instance ) {
        // Output admin widget options form
        $title = esc_attr($instance['title']);

        require('inc/widget-fields.php');
    }
}

function wptreehouse_badges_register_widgets() {
    register_widget( 'Wptreehouse_Badges_Widget' );
}

add_action( 'widgets_init', 'wptreehouse_badges_register_widgets' );

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

options-page-wrapper.php

<h2><?php esc_attr_e( '2 Columns Layout: static (px)', 'wp_admin_style' ); ?></h2>

<div class="wrap">

    <div id="icon-options-general" class="icon32"></div>

    <h1><?php esc_attr_e( 'Treehouse Badges Plugin', 'wp_admin_style' ); ?></h1>

    <div id="poststuff">

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

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

                <h2>Treehouse Badges Plugin.</h2>

                <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( 'Let\'s Get Started', 'wp_admin_style' ); ?></span>
                        </h2>

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

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

                            <div class="inside">
                                <table class="form-table">
                                    <tr>
                                        <td class="row-title"><label for ="wptreehouse_username">Treehouse Username: </label></td>
                                        <td><input type="text" name="wptreehouse_username" value="" class="regular-text" /></td>                                    
                                    </tr>                                               

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

                        </form> 
                        <p>
                            <input class="button-primary" name="wptreehouse_username" type="submit" value="<?php esc_attr_e( 'Save Username' ); ?>" />
                        </p>
                    </div>
                    <!-- .postbox -->
                    <?php else: ?>


                    <div class="postbox">

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

                        <ul class="wptreehouse-badges">

                        <p>Most Recent Badges</p>   

                            <?php for( $i = 0; $i < 20; $i++ ): ?>
                                <li>
                                    <ul>
                                        <li>
                                            <img class="wptreehouse-gravatar" width="120px" src="
<?php echo $wptreehouse_profile->{'badges'}[$i]->{'icon_url'}; ?>">                             
                                        </li>

                                        <?php if ($wptreehouse_profile->{'badges'}[$i]->{'url'} != $wptreehouse_profile->{'profile_url'} ): ?>                                      
                                        <li class="wptreehouse-badge-name">
                                            <a href="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'name'}; ?>"><?php echo $wptreehouse_profile->{'badges'}[$i]->{'name'}; ?></a>
                                        </li>
                                        <li class="wptreehouse-project-name">
                                            <a href="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[1]->{'title'}; ?>"><?php echo $wptreehouse_profile->{'badges'}[$i]->{'courses'}[1]->{'title'}; ?></a>
                                        </li>

                                        <?php else: ?>

                                        <li class="wptreehouse-badge-name">
                                            <?php echo $wptreehouse_profile->{'badges'}[$i]->{'name'}; ?>
                                        </li>

                                        <?php endif ?>
                                    </ul>                                   
                                </li>                               
                            <?php endfor; ?>

                        </ul>
                    </div>              
                    <!-- .postbox -->
            <?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 -->

                        <h2 class="hndle"><span><?php esc_attr_e(
                                    'Sidebar Content Header', 'wp_admin_style'
                                ); ?></span></h2>

                        <div class="inside">
                            <h3><span><?php echo $wptreehouse_profile->{'badges'}[$i]->{'name'}; ?>'s Profile</span></h3>
                                <div class="inside">

                                    <p><img width="100%" class="wptreehouse-gravatar" src="<?php echo $wptreehouse_profile->{'badges'}[$i]->{'gravatar_url'}; ?>" alt="Mike the Frog Gravatar"></p>

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

                                <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>

                                </ul>

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

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

                            <p>
                                <label for ="wptreehouse_username">Username: </label>
                            </p>            
                            <p>
                                        <input type="text" name="wptreehouse_username" value="<?php echo $wptreehouse_username; ?>" />
                                        <input type="submit" name="wptreehouse_username_submit" value="Update" class="button-primary" />

                            </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 -->

Anyone any idea what's causing it? I'm so close to compelting the track but I'd rather not havw to go back through the course ifI can help it. :) Thanks