Widget Array – How to use button to add element to array inside widget and update display? How to update?

Question

I have this widget

enter image description here

When we press “Add Scene” button widget assume will look like this…

enter image description here

And here’s form code:

    function form( $instance ) {
    $scenes = isset ($instance['scenes']) ? $instance['scenes'] : array();
    $scenes_counter = 0;

    foreach($scenes as $scene){
        $inner_scenes = isset ($instance['scenes']['inner_scene']) ? $instance['scenes']['inner_scenes'] : array(
            'scene_id' => 'scene_0'.($scenes_counter+1),
            'envMapURL' => 'snvironment Map',
            'modelURL' => 'model URL',
            'modelScale' => 'model Scale',
            'twoSideMats' => 'two side Materials list'
        );

        $fields = array();

        $fields['scene_id'] = sprintf(
            '<input class ="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s" disabled>',
            $this->get_field_id('scene_id'. '-0' . $scenes_counter),
            $this->get_field_name('scene_id'),
            $scenes_counter,
            esc_attr($inner_scenes['scene_id'])
        );
        $fields['envMapURL'] = sprintf(
            '<input class ="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">',
            $this->get_field_id('envMapURL'. '-0' . $scenes_counter),
            $this->get_field_name('envMapURL'),
            $scenes_counter,
            esc_attr($inner_scenes['envMapURL'])
        );     
        $fields['modelURL'] = sprintf(
            '<input class ="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">',
            $this->get_field_id('modelURL'. '-0' . $scenes_counter),
            $this->get_field_name('modelURL'),
            $scenes_counter,
            esc_attr($inner_scenes['modelURL'])
        );          
        $fields['modelScale'] = sprintf(
            '<input class ="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">',
            $this->get_field_id('modelScale'. '-0' . $scenes_counter),
            $this->get_field_name('modelScale'),
            $scenes_counter,
            esc_attr($inner_scenes['modelScale'])
        );      
        $fields['twoSideMats'] = sprintf(
            '<input class ="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">',
            $this->get_field_id('twoSideMats'. '-0' . $scenes_counter),
            $this->get_field_name('twoSideMats'),
            $scenes_counter,
            esc_attr($inner_scenes['twoSideMats'])
        );     
        
        $scenes_counter++;

        print '<b><i>SCENE 0' . $scenes_counter . '</i></b><br /><br />' . join('<br /><br />', $fields) . '<br /><br />';
    }

    print '<input type="button" value="Add Scene" onlick="">';

    }

I follow this How to build widget with arrays inside arrays?.

I try to learn. But in that question doesn’t specify how to use button to add an element into array.

Secondly, I attempt to use array_push($scenes, 1, 2); for testing purpose and use that answer method to update but it doesn’t work.


SUMMARIES:

  1. How could I use a button form to add an elements and update display for edit?
  2. How could I update this widget?

Thanks in advanced!

0
user213201 2 months 2021-10-04T08:26:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse