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

Bartosz Kubisz
Bartosz Kubisz
26,277 Points

How to Build a Wordpress Plugin. Challenge Task 4 of 6

Question: 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.

My code

<?php

    class My_Plugin_Widget extends WP_Widget {

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

    function widget( $args, $instance ) {
        // Widget output
        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' );
    }
    }
?>

Prompt Bummer! The first thing inside the widget method, use the extract function to make $args into local variables.

What Iā€™m doing wrong?

plugin.php
<?php

    class My_Plugin_Widget extends WP_Widget {

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

    function widget( $args, $instance ) {
        // Widget output
        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' );
    }

    }



?>
Michelle Martinez
Michelle Martinez
4,263 Points

removing the comment... great

Henrik Hansen
Henrik Hansen
23,176 Points

Omg, that was just so annoying! Thank you Michelle! You should add that as an answer.