$ instance in widget wordpress form method is null

I am new to Wordpress widget development and am learning tutsplus videos on the go. I am trying to create a simple widget for my site. So far I have been following every step of the video, but still I get the following error and the form does not save the value.

Warning: extract() expects parameter 1 to be array, null given in C:\Program Files\Ampps\www\test\wp-content\plugins\first\index.php on line 50 

This is the from method.

 public function form($instance){ extract($instance); ?> <p> <lable for="<?php echo $this->get_field_id('ap_title_text');?>">Title Text: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" /> </p> <?php 

So far, I just created a constructor and registered a widget. No errors were detected before this step in the video. And every site I was looking for for this problem showed similar steps. I do not understand why it shows an error in my system.
I am using wordpress 3.5.2, downloaded yesterday and using php5.3.

EDIT :: Here is the code.

 class SidebarWidget extends WP_Widget{ function __construct() { $options = array( 'description' => 'Simplest way to start working from any page', 'name' => 'Sidebar Widget' ); parent::__construct('appSidebarWidget', '', $options); } public function form($instance) { extract($instance); ?> <p> <label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" /> </p> <p> <label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >app Name: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" /> </p> <?php } public function widget($args, $instance) { extract($args); extract($instance); $display_gadget = "<iframe src='http://$appURL' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>"; if(empty($ap_title_text)){$ap_title_text = "Schedule Now";} echo $before_widget; echo $before_title.$ap_title_text.$after_title; echo $display_gadget; echo $after_widget; } } add_action('widgets_init','appRegisterWidget'); function appRegisterWidget() { register_widget('appSidebarWidget'); } 

I still failed to get it to work. However, the actual project that I studied works as expected. I would still like to know what is wrong with this. Because of this, I almost lost my job.

Also, can anyone lead me to call a custom javascript function using a button on the displayed widget. Basically, my goal is to display a button on widgets displayed by form information. When someone clicks a button, he should display an overlay with a message.

+4
source share
4 answers

In the following code

 add_action('widgets_init','appRegisterWidget'); function appRegisterWidget() { register_widget('appSidebarWidget'); <-- } 

Change it to

 register_widget('SidebarWidget'); 

I added the text box url in the widget setup form and worked (the full modified code is given below)

 class SidebarWidget extends WP_Widget{ function __construct() { $options = array( 'description' => 'Simplest way to start working from any page', 'name' => 'Sidebar Widget' ); parent::__construct('appSidebarWidget', '', $options); } public function form($instance) { extract($instance); ?> <p> <label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" /> </p> <p> <label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >App Name: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" /> </p> <!-- New text box added --> <p> <label for="<?php echo $this->get_field_id('ap_app_url'); ?>" >App Url: </lable> <input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_url');?>" name="<?php echo $this->get_field_name('ap_app_url');?>" value="<?php if(isset($ap_app_url)) echo esc_attr($ap_app_url);?>" /> </p> <?php } public function widget($args, $instance) { extract($args); extract($instance); $display_gadget = "<iframe src='http://" . $ap_app_url . "' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>"; if(empty($ap_title_text)){$ap_title_text = "Schedule Now";} echo $before_widget; echo $before_title.$ap_title_text.$after_title; echo $display_gadget; echo $after_widget; } } add_action('widgets_init','appRegisterWidget'); function appRegisterWidget() { register_widget('SidebarWidget'); } 

Screenshot of a working example below

enter image description here

+2
source

The code crashes because the $instance variable is not an array. The extract() function must get an array to do this job. It is very clear and very simple.

The problem is that no one can help you explain why $instance is null if you did not provide code that calls the form() function. There could be a million reasons why $instance not an array.

0
source

The form () function is called by the WP_Widgets object.

0
source

$instance also always be empty in the form method if you have the empty update function. This may not apply to this issue, but please check if this is a problem if you encounter a similar problem.

0
source

Source: https://habr.com/ru/post/1490170/


All Articles