How to put a variable in a instance in the widget


I am creating a widget that handles order numbers. The basic structure is as follow:

// pretty generic widget functions
public function widget($args, $instance)
  $order_number = $instance['order_number'];
public function update($new_instance, $old_instance)
  $instance = $old_instance;
  $instance['order_number'] = strip_tags($new_instance['order_number']);

public function form($instance)
 if ($instance) {
    $order_number = esc_attr($instance['order_number']);
 }else {
    $order_number = '';

what I usually do is to ask the user for the order number. Like this:

<input name="<?php echo $this->get_field_name('order_number'); ?>" 
type="text" value="<?php echo $order_number; ?>" />

but now I want to have a function that generates order_number. So my function is

function order_generator($email){
     return $order_num;

I want order_num to be set for the value of order_number.

how can I do this?

Update #1

As requested by kaiserI try to explain this briefly.

First Case:
I have a widget that prints the order number. This order number is entered by the user in an input field. As explained in the question, once the user enters an order_number it is saved in the instance and can be displayed later.

Now this is the question: I created a function called order_generator that dynamically creates the order number. The user doesn’t have to enter the order_number. The function returns an integer named $order_num. I want to remove that input field which asked the user for order number and make the order_num value to order_number so I can display it.

, , , Kyle 6 years 2016-08-20T15:18:55-05:00 0 Answers 82 views 0

Leave an answer