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

4/6 - WordPress Widgets Code Challenges

"Inside of the widget function, below the extract function, apply the widget_title filter to get the title of the instance. Assign it to a variable named title."

I feel like I'm missing something obvious. I've got the right variable name, $title is in the right location, the filter appears to be written correctly.

What am I missing?

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

Thanks

4 Answers

Gareth Borcherds
Gareth Borcherds
9,372 Points

Your code is correct. However, when I went through the challenge, I got an error and then I moved the code to different lines to make it work. Here is my full code that passed that test. I deleted the comments on that section and put the extract there and then got it to work. Maybe there's something weird with how they check the result there?

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






?>

That was quite the ordeal. Must have been a bug. I guess a few code challenges would be buggy out of dozens.

Thanks for your help.

sondus zatar
sondus zatar
8,604 Points

Below the My_Plugin_Widget class, add a function named my_plugin_register_widgets that registers the widget.

here is my code ... not working ... why ?

class My_Plugin_Widget extends WP_Widget {

function my_plugin_register_widgets() {
register_widget( '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 ) {
    // 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');
}



}