0
votes

I am new to WordPress plugin development. I have following codes for form method of plugin. When I enter anything in title field and save, it gives, Warning: extract() expects parameter 1 to be array, null given in /Applications/XAMPP/xamppfiles/htdocs/NGO/wp-content/plugins/messagner

 public function form($instance)
        {
                extract($instance);
                print_r($instance);
                ?>
                <p>
                        <label for="<?php echo $this->get_field_id('title');?> ">Title</label>
                        <input 
                        class="widefat"
                        id = "<?php echo $this->get_field_id('title');?>"
                        name = "<?php echo $this->get_field_name('title');?>"
                        value = "<?php if(isset($title)) echo esc_attr($title); ?>"
                        />
                </p>
                <p>
                        <label for="<?php echo $this->get_field_id('description'); ?>"></label>
                        <textarea 
                        class = "widefat"
                        rows = 10
                        id = "<?php echo $this->get_field_id('description');?>"
                        name = "<?php echo $this->get_field_name('description'); ?>"
                        value = "<?php if(isset($description)) echo esc_attr($description); ?>"
                        >
                </textarea>
                       </p>
                <?php

        }?>

I don't know where I am making some silly mistake please help. Thanks.

1
extract converts an array to variables: extract(array("demo" => "Hello World")); is $demo = "Hello World"; what makes print_r($instance);? Please tell us the content. - Adrian Preuss
try to debug your $instance variable that you are using in extract($instance); - jogesh_pi

1 Answers

1
votes

HAVE A LOOK AT THIS CODE and follow, $instance is returned to be null in this case. It should get array .

 <?php
    /**
     * Example Widget Class
     */
    class example_widget extends WP_Widget {


        /** constructor -- name this the same as the class above */
        function example_widget() {
            parent::WP_Widget(false, $name = 'Example Text Widget');    
        }

        /** @see WP_Widget::widget -- do not rename this */
        function widget($args, $instance) { 
            extract( $args );
            $title      = apply_filters('widget_title', $instance['title']);
            $message    = $instance['message'];
            ?>
                  <?php echo $before_widget; ?>
                      <?php if ( $title )
                            echo $before_title . $title . $after_title; ?>
                                <ul>
                                    <li><?php echo $message; ?></li>
                                </ul>
                  <?php echo $after_widget; ?>
            <?php
        }

        /** @see WP_Widget::update -- do not rename this */
        function update($new_instance, $old_instance) {     
            $instance = $old_instance;
            $instance['title'] = strip_tags($new_instance['title']);
            $instance['message'] = strip_tags($new_instance['message']);
            return $instance;
        }

        /** @see WP_Widget::form -- do not rename this */
        function form($instance) {  

            $title      = esc_attr($instance['title']);
            $message    = esc_attr($instance['message']);
            ?>
             <p>
              <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
              <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
            </p>
            <p>
              <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Simple Message'); ?></label> 
              <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
            </p>
            <?php 
        }


    } // end class example_widget
    add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
    ?>