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 Building WordPress Widgets, and Shortcodes WordPress Widgets Code Challenges

sondus zatar
sondus zatar
8,604 Points

Below the my_plugin_widget function, write out the three additional functions required for creating a widget.

Below the my_plugin_widget function, write out the three additional functions required for creating a widget. Don't forget to pass the correct parameters based on the naming conventions used in the Treehouse plugin. Leave the functions empty?

5 Answers

Daniel Johnson
Daniel Johnson
104,132 Points

You have the my_plugin_register_widgets function inside the My_Plugin_Widget class. It belongs outside of the class, and you also had the class name as WP_Widget when it should say My_Plugin_Widget. Then you need the add_action function after that to enable the widget. I provided the full solution in case you run into any other issues.

plugin.php
<?php

// Widget class
class My_Plugin_Widget extends WP_Widget {

    // Instantiate the parent object
    function my_plugin_widget() {
        parent::__construct(false, 'My Plugin Widget');
    }

    // Widget output
    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
    }

    // Save widget options
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }

    // Output admin widget options form
    function form($instance) {
        $title = esc_attr($instance['title']);
        require('inc/widget-fields.php');
    }

}

// Register the widget
function my_plugin_register_widgets() {
    register_widget('My_Plugin_Widget');
}

// Enable the widget
add_action('widgets_init', 'my_plugin_register_widgets');

The question is not worded correctly for the first task

Task1 Answer <?php

class My_Plugin_Widget extends WP_Widget {

    function my_plugin_widget() {
        parent::__construct( false, 'My Plugin Widget' );
    }
function widget( $args, $instance ) {}
function update( $new_instance, $old_instance ) {}
function form ( $instance) {}


}

?>

Task 2 / 3 <?php

class My_Plugin_Widget extends WP_Widget {

    function my_plugin_widget() {
        parent::__construct( false, 'My Plugin Widget' );
    }
function widget( $args, $instance ) {}

function update( $new_instance, $old_instance ) {
  $instance = $old_instance;
  $instance['title'] = strip_tags($new_instance['title']);
    return $instance;         
}
function form ( $instance) {
$title = esc_attr($instance[ 'title' ]);
  require ( 'inc/widget-fields.php'); 
                  }


}

?>

task 5 <?php

class My_Plugin_Widget extends WP_Widget {


    function my_plugin_widget() {
        parent::__construct( false, 'My Plugin Widget' );
    }
function widget( $args, $instance ) {
  extract( $args );
  $title = apply_filters( 'widget_title', $instance['title']);
}

function update( $new_instance, $old_instance ) {
  $instance = $old_instance;
  $instance['title'] = strip_tags($new_instance['title']);
    return $instance;         
}
function form ( $instance) {
$title = esc_attr($instance[ 'title' ]);
  require ( 'inc/widget-fields.php'); 
                  }

}

function my_plugin_register_widgets() { register_widget('My_Plugin_Widget');

}

?>