Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been working on my first widget, Everything works perfectly except it doesn't save anything I add to the textarea box. It will output the input on the website however.

From research I think its something to do with the form function and update, but can't quite get my head around which part.

Here is my widget code.

<?php

 /*
Plugin Name: Hexagran Featured Widget
Plugin URI: http://thevisionists.com
Description: Produces a widget for use in the widget panel.
Version: 0.1
Author: Danny & Tom
Author URI: http://thevisionists.com
License:none


 */

 //$args pulls in the arguments from the sidebar.php

    class hexagram_featured_widget extends WP_Widget {

        function __construct() {
            parent::__construct('false', $name = __( 'Hexagram Featured Box'), //tranlation enabled // accessing the perant constructer
            array( 'description' => __('Displays the content you place inside, strips all html'))
            ); // end of the construct
        }

        function form($instance) {
            $title = strip_tags( $instance['title'] );
            $info = esc_textarea( $instance['info'] );

            ?>
            <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 esc_attr($title); ?>" /></p>
        <p><label for="<?php echo $this->get_field_id('info'); ?>"><?php _e('Text Goes here:'); ?></label>
        <textarea class="widefat" rows="8" cols="10" id="<?php echo $this->get_field_id('info'); ?>" name="<?php echo $this->get_field_name('info'); ?>"></textarea>
            <?php
        }

        function update( $new_instance, $old_instance) {
            $instance = array();
            $instance[ 'title' ] = strip_tags($new_instance[ 'title' ]);
            $instance[ 'info' ] = strip_tags($new_instance[ 'info' ]);
            return $instance;
        }

        function widget($args, $instance) {
            extract($args);
            $title = apply_filters('widget_title', $instance['title'] );
            $info = $instance[ 'info' ];

            echo $before_widget;
            if ( ! empty( $title ) ) echo $before_title . $title . $after_title;
            if ($info) print $info;
            echo $after_widget;


        }

    } // end of the class holding all the functions


    add_action('widgets_init', function() {
        register_widget('hexagram_featured_widget');
    }
    )
     // adds an action to register the widget
    // class name is always the name of the register widget argument.
?>
share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Your primary issue isn't with saving the data but displaying in the form if it has been saved. I rewrote the offending function:

  function form($instance) {
    $title = strip_tags( $instance['title'] );
    $info = esc_textarea( $instance['info'] );
    // var_dump($instance); // debug
    ?>
    <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 esc_attr($title); ?>" />
    </p>
    <p>
    <label for="<?php echo $this->get_field_id('info'); ?>"><?php _e('Text Goes here:'); ?></label>
      <textarea class="widefat" rows="8" cols="10" id="<?php echo $this->get_field_id('info'); ?>" name="<?php echo $this->get_field_name('info'); ?>"><?php if (!empty($info)) echo $info; ?></textarea>
    <?php
  }

You should make the change suggest by @br3nt, though.

I don't see any other issues except for some minor notices that you would see if you had debugging enabled

share|improve this answer
    
I've implemented the change made by br3nt, I really appreciate you helping me as I'm only a novice with the widget api :) –  Dannyw24 Jun 23 '13 at 21:45
    
This code will work then s_ha_dum? –  Dannyw24 Jun 23 '13 at 22:25
    
It works for me. It is now up to you to test it and see if it does what you need. –  s_ha_dum Jun 23 '13 at 22:27
    
it works great thankyou! ohh I see you added the if not empty then echo $info; –  Dannyw24 Jun 23 '13 at 22:33
add comment

try adding:

$instance = $old_instance;

as the first line of your update function, instead of:

$instance = array();
share|improve this answer
    
Thanks for the help br3nt, your suggestion didn't seem to work. –  Dannyw24 Jun 23 '13 at 21:24
    
ahhhh yes.... I assumed that data was dissappearing because you were only saving two fields from the form, didn't look close enough to see that you ONLY HAD those two fields in your form. –  br3nt Jun 23 '13 at 21:49
add comment

Change your class name which is

class hexagram_featured_widget extends WP_Widget {

to this

class WP_Widget_hexagram_featured_widget extends WP_Widget {

and this line

parent::__construct('false', $name = __( 'Hexagram Featured Box'), //tranlation enabled // accessing the perant constructer

to this

parent::__construct('hexagram_featured_widget', $name = __( 'Hexagram Featured Box'), //tranlation enabled // accessing the perant constructer

and lastly change this line

add_action('widgets_init', function() {
        register_widget('hexagram_featured_widget');
    }

to

add_action('widgets_init', function() {
        register_widget('WP_Widget_hexagram_featured_widget ');
    }
share|improve this answer
    
Why change the class name? And PHP does not support named parameters (like, say, Python) so I am not sure why are using that syntax. And you have a missing ). How does any of this address the problem in the question? –  s_ha_dum Jan 5 at 1:36
    
I have had same problem. When I looked the wordpresses class names, they starts name of class with the WP_Widget_.... that have worked for me. –  ewroman Jan 5 at 9:53
    
The class names are by convention. There is no reason the names should effect how the code works. I suspect you had a typo somewhere that was fixed when you changed the names. –  s_ha_dum Jan 5 at 14:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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