I’m attempting to create my first ever widget and was hoping someone could explain the difference between $this and $instance used in the following widget form code.

<?php
public function form($instance){ ?>
    <label for="<?php echo $this->get_field_id('title'); ?>">Title: 
        <input type="text" 
               name="<?php echo $this->get_field_name('title'); ?>" 
               value="<?php echo $instance['title']; ?>" 
               id="<?php echo $this->get_field_id('title'); ?>"
        />
    </label>  
<?php 
}

1 Answer
1

$this is the widget object, an instance of the relevant widget class. It includes things like the widget ID and class identifiers which are required for the method that generate the input id and name attribute in the snippet

$instance is the array which includes the widget setting. The settings ae not stored as part of the object and therefor needs to be passed as a parameter

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *