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 Adding Settings to a WordPress Widget

Nicolas Blanco Mattos
PLUS
Nicolas Blanco Mattos
Courses Plus Student 2,545 Points

$instance: how does it know the info of badges?

I'm missing something... We have created the class for the widget... but we are not telling the class anywhere to fetch the badges info (as we have donde fot the plugin settings page...)

How is it possible that $instance gets that info automatically?

3 Answers

Brian Hayes
Brian Hayes
20,986 Points

I think you may be mixing up what data is stored in $instance and what data is stored via the Options API, and I think my first answer may not have helped at all in that case.

$instance are options that pertain to the specific widget that is in use. Say you have 3 or 4 of these widget throughout your site, well that $instance is not universal to all of them. When you update the one of the widget forms, the function will run for that widget and the data will run through $instance as you see it in your functions, but it only pertains to that specific instance of the widget. This is much more the Widgets API than the Options API at work here. This is why the only thing you would have stored via $instance would be options that you need for the presentation of the widget as it will display on the frontend, all for the sake of the individual widget, and not for all widgets of the same type.

What you store via the options page and via the Options API functions is data that is universal to all Badges widgets in use on the site. So, $instance has nothing to do with it, and will never actually interact with it. What happens is that the options stored in $instance are used in the code to conditionally handle how we present the a specific widget.

It's also important to remember that the option of wptreehouse_badges is just the name of where the options for this plugin are accessible via the Options API. This doesn't mean that badges are stored there. From what I can see in the plugin's wptreehouse_badges_options_page() function, $options['wptreehouse_profile'] is actually storing the entire JSON feed retrieved from the AJAX call. So, by accessing that variable alone, you have the ability to traverse the object and get whatever data that exists in the stream.

This is actually happening in the widget method:

<?php
function widget( $args, $instance ) {
    // Widget output.

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

    // Right here you can see we are getting all of the data stored in `wptreehouse_badges`.
    $options = get_option( 'wptreehouse_badges' );

    // And here we are assigning the entire JSON stream in the retrieved options data in `$wptreehouse_profile`.
    $wptreehouse_profile = $options['wptreehouse_profile'];

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

}

Hopefully this helps a bit. Let me know if you have any more questions, or just if my explanation is no help at all, lol.

Nicolas Blanco Mattos
Nicolas Blanco Mattos
Courses Plus Student 2,545 Points

Thanks, it makes a little mote sense now! I'll dig deeper into widgets api then to understand it better, but you helped a lot. I didn't know the code was applied to the widget we are working in that moment (I imagine this is handled by other classes behind the scenes), so thanks!

Brian Hayes
Brian Hayes
20,986 Points

You're not actually creating a new class from scratch. You're extending a class that already exists to create this new class. So basically, you're extension on that class just adds some extra details specific to the widget you're creating, however, all of the methods and properties that exist in WP_Widget are still used. So you can think of you're new class as all of the code you would see if you looked at the WP_Widget class in WordPress core, but with the widget, update, and form methods updated to what you wrote in the extension.

So, to answer your specific question: There are methods in place to handle $instance for you in WP_Widget. You just need to provide the code you wrote in your extension for it to work. This saves you from having to write the kinds of methods required to store information in the database, as well as query the database for that data.

Nicolas Blanco Mattos
PLUS
Nicolas Blanco Mattos
Courses Plus Student 2,545 Points

Joey Hayes Thanks! I actually understrand that part. We are extending the WP_Widget class, not creating a new one from scratch. What I don't understand is inside that WP_Widget class we are extending, what the method that tells this widget we are using the data from treehouse badges is.

I mean, we have the construct() method, widget() method, update() method, and form() method. The only one where we are actually pulling data from thedatabase about the badges is widget(). The other ones are just using the $instance variable with all the correct data inside it.

Let me say this in another way. Where am I telling this widget to looj for another data? Imagine I need to store personal data of the user instead of the badges. This personal data is stored in 'wp_options' too but is named 'wp_personal_data'. Where is the code to make this widget use that data insetad of the badges? In this case, $instance should be all the info that I store as 'wp_personal_data'...

Does it make sense?