0

I have very simple wordpress widget, that has 3 input field. Whenever i submit data, it does not save to database. I have added and deleted the plugin , deactivated and reactivated multiple times, but no luck. Below is my code :-

    class price_package extends WP_Widget {

    function  __construct() {
        parent::__construct(
            'price_package',
            'Price Package',
            array(
                'classname' => 'price-package',
                'description' => 'Wordpress Package Page Creator'
            )
        );
        add_action('admin_print_styles', array($this, 'register_admin_styles'));
        add_action('wp_enqueue_scripts', array($this, 'register_widget_styles'));
        add_shortcode('package', function($atts, $content = null) {
            $atts = shortcode_atts(
                array(
                    'name' => 'Super Platinum',
                    'content' => 'No description available.'
                ), $atts
            );
            extract($atts);
            include(plugin_dir_path(__FILE__).'/views/package.php');
        });
    }

    function form ($instance) {
        $instance = wp_parse_args(
            (array)$instance,
            array (
                'name' => '',
                'divclass'  => '',
                'paypal' => '',
            )
        );
        include(plugin_dir_path(__FILE__).'/views/admin.php');
    }

    function update($new_instance, $old_instance) {
        $old_instance['name'] = htmlentities($new_instance['name']);
        $old_instance['divclass'] = htmlentities($new_instance['divclass']);
        $old_instance['paypal'] = htmlentities($new_instance['paypal']);
        return $old_instance;
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);
        echo $before_widget;
        include(plugin_dir_path(__FILE__).'/views/widget.php');
        echo $after_widget;
    }

    function register_admin_styles() {
        wp_enqueue_style('price-package', plugins_url('price-package/css/admin.css'));
    }

    function register_widget_styles() {
        wp_enqueue_style('price-package', plugins_url('price-package/css/widget.css'));
    }
}

add_action('widgets_init', create_function('', 'register_widget("price_package");'));

Let me know, if i am doing some mistake.

  • shouldn't return $old_instance; be return $new_instance;? – Nathan Powell Aug 5 '15 at 15:16
  • I also tried changing $old_instance to $new_instance , But no change. – John Cargo Aug 5 '15 at 15:35
1

The problem is your update function. $old_instance is kind of special variable. The simplest is

function update($new_instance, $old_instance) {
    return $new_instance;
}

For the safe

function update($new_instance, $old_instance) {
        $instance = array();
        $instance['name'] = htmlentities($new_instance['name']);
        $instance['divclass'] = htmlentities($new_instance['divclass']);
        $instance['paypal'] = htmlentities($new_instance['paypal']);
        return $instance;
    }
| improve this answer | |
  • I did exactly what you have done , but still it is not saving. – John Cargo Aug 5 '15 at 15:35
  • 1
    So, it is in the file of this one include(plugin_dir_path(FILE).'/views/admin.php'); the input fields in the form must have the name attribute the same as 'name', 'divclass', 'paypal' – Trang Aug 5 '15 at 15:46
  • oh my god! i just forgot to use echo. i replaced my code <?php $this->get_field_id('name'); ?> to <?php echo $this->get_field_id('name'); ?> – John Cargo Aug 5 '15 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.