I am new to wordpress widget development and learning from tutsplus videos on the go. I am trying to create a simple widget for my website. So far I am following every steps from the video but still I am getting following error and the form is not saving the values.
Warning: extract() expects parameter 1 to be array, null given in C:Program FilesAmppswwwtestwp-contentpluginsfirstindex.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 have just created the constructor and registered the widget. There was no error displayed till this step in the video. And every website I googled for this problem showed similar steps. I don’t understand why it is showing error on 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 haven’t been able to get it working. However the actual project for which i was learning this, works as expected. I would still like to know what is wrong with this one. I nearly lost my job because of this.
Also, can anyone guide me to call a custom javascript function from a button on widget being displayed. Basically my purpose is to display a button on the widget rendered by the information on the form. When someone click the button, It should display an overlay with message.
In the following code
Change it to
I’ve added a url
text box
in the widget setup form and it’s working, (full modified code given below)Screen shot of working example given below
The code is crashing because the variable
$instance
is not an array. Theextract()
function has to receive an array in order to do it’s job. That much is clear and very simple.The problem is that nobody can help you explain why
$instance
is null unless you provide the code which calls theform()
function. There could be a million reasons why$instance
is not an array.The form() function is being called by the WP_Widgets object.
$instance
could also always be null in the form method if you have an empty update function. This may not relate to this question, but please check whether that is the issue if you run into a similar issue.