• Hi All,

    I am assuming there is a more compact way to handle a widget with many options.

    My widget has about 20 options.

    In each subclass of WP_Widget, they need to be defined/used. The instance param, as I can understand it, is a list of every option (not sure why this is not an array) and is used in the following subclasses:

    function widget($args, $instance)
    function update($new_instance, $old_instance)
    function form($instance)

    For example, in function widget:

     $param1=$instance['param1'];
     $param2=$instance['param2']
     etc...

    In function update:

     $instance['param1']=strip_tags($new_instance['param1']);
     $instance['param2']=strip_tags($new_instance['param2']);
     etc...

    In function form:

     $param1=esc_attr($instance['param1']);
     $param2=esc_attr($instance['param2']);
     etc...

    The code gets very bulky. Is there a better, more compact way to handle this? A standard way to loop through instances and get the names?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    $instance is an array. $instance['param1'] is an array reference. Maybe you are wondering why it’s not an object, referenced as $instance->param1?

    Useful functions that you should find useful are extract() and array_map().

    Thread Starter chz516

    (@chz516)

    I misread and didnt didnt see it labeled as an array in the docs (but it is) which is why it seemed strange that it was being referenced as an array.

    I guess what I was hoping to find was a way to access the variable name. This way you could loop through them and condense all the code instead of explicitly refering to each key. It doesnt look like extract() will do that, but will get the values. As for array_map()… I cant even understand it! Im new to PHP so was hoping to find a way to reference the variable names of the array, but i guess no languages really do that. I see the variable variables, but many of the comments i have seen mention how quickly it begins to confuse the code.

    Maybe just hardcoding the long way makes more sense in this case? Although I cant be the first one to use a lot of options, and Im sure many use far more. What woulb be best practice to optimize in this case?

    You can use a standard foreach() loop like this:

    foreach( $instance as $key => $val ){
        // Do something based on the value for 'key'
        switch( $key ){
            case 'option1':
                doSomething();
                break;
            default:
                doSomethingElse();
        }
    }

    As for best practice, there’s not really one. If you are going to have standard processing for each option, then a loop like this will be a good idea, but if every option needs to be handled/processed differently, then you could be best off doing it individually. It’s really up to how you’re going to be most comfortable with it.

    Dion

    (@diondesigns)

    For function widget:

    extract($instance);
    

    For function update:

    $instance = array_map('strip_tags', $new_instance);
    

    For function form:

    extract(array_map('esc_attr', $instance));
    
    Thread Starter chz516

    (@chz516)

    Ok, the code works exactly. The array_map to set the instances in each function and the foreach to loop through for a bunch of echo statements.

    My widget is pretty simple PHP-wise. I developed in javascript, not intended originally for WP and decided to port it over. So really the PHP is just a wrapper for the js. Still, because of all the options, the code was getting bulky. It is now really small and condensed! So somebody here is a genius. Either me or you guys ??

    Thanks so much. What a huge help. I am new to PHP (I use ASP/vbscript) so was struggling with the syntax and structure a bit.

    • This reply was modified 6 years, 9 months ago by chz516.
    • This reply was modified 6 years, 9 months ago by chz516.
    Thread Starter chz516

    (@chz516)

    Ok, one last thing I cant make sense of. In function form, I have many inputs to be created. They are all type=text so I figured I could call a function to build each form field. Something like:

    createTextbox('Field One');
    createTextbox('Field Two');
    etc;
    
    function createTextbox($fldname){
       ...use fldname to create the textbox
    }

    Now code as it is now, uses the below to build a form field. So, I just need to move this into a function…

    function form($instance) {	
    		extract(array_map('esc_attr', $instance));
    <?php/*<p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('align:'); ?></label><input class="widefat" id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>" type="text" value="<?php echo $align; ?>" /></p> */?>

    But I cant figure out what $this means in this code. I know (think) it represents
    the current object, but there is no current object here that I can see. If I were looping through $instance, it would make sense. But what is $this here? I can see that I need $this to be sent into the function… but just dont get it.

    For widgets, $this is the s the widget object (from the widgets class), and $instance is the values set for that widget object.

    If you’re doing this the right way, you’ve got this set up as a class, don’t you? With classes, anything that’s $this is the object instance, and is accessible throughout the classes functions without needing anything extra. All its’ doing in the case of widgets are setting up the helper functions for field ID’s and names.

    Thread Starter chz516

    (@chz516)

    Ohhh…. I think I get it.

    Yes it is set up as a class (because that was what the code sample I found had)

    This clears up so much for me now. Although, now I wonder why I see $instance is passed around to various functions. Shoulnt it be accessible everywhere, just like $this?

    Nope. $this is a standard PHP construct so you can get the various values and functions of that class in the object that you create. The $instance value is just a name that WordPress uses for the data that’s been set for that object, so it needs to be passed around manually. It does take a little bit of understanding of how programming works internally, but when you get to know the concepts of Object Oriented programming a bit more, it will make more sense. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to handle $instance when you have a lot of widget options’ is closed to new replies.