Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been following along a Lynda.com tutorial on plugin development and copied the plugin in the video to get an understanding of how they work. When I try to save the simple widget which basically just puts a title and some text in a widget area.

Following the code from the tutorial found here (Chapter 4) this is what I have.

class SimpleWidget extends WP_Widget
{
    function SimpleWidget()
    {
        $widget_options = array(
            'classname' => 'simple-widget',
            'description' => 'Just a simple widget'
        );

        parent::WP_Widget('simple_widget','Simple Widget', $widget_options);
    }

    function widget($args, $instance)
    {
        extract($args, EXTR_SKIP);
        $title = ($instance['title']) ? $instance['title'] : 'A Simple Widget';
        $body = ($instance['body']) ? $instance['body']: 'A simple message';
        ?>
        <?php echo $before_widget; ?>
        <?php echo $before_title . $title . $after_title; ?>
        <p><?php echo $body; ?></p>
        <?php   
    }



    function form( $instance )
    {
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>">
        Title: </label><br />
        <input id="<?php echo $this->get_field_id('title'); ?>" 
                name="<?php $this->get_field_name('title'); ?>"
                value="<?php echo esc_attr($instance['title']); ?>" /></p>


        <p><label for="<?php echo $this->get_field_id('body'); ?>">Body:</label><br />
            <textarea id="<?php echo $this->get_field_id('body'); ?>" 
                name="<?php $this->get_field_name('body'); ?>" ><?php echo esc_attr($instance['body']); ?></textarea></p>  
        <?php
    }
}

function simple_widget_init()
{
    register_widget("SimpleWidget");
}

add_action('widgets_init', 'simple_widget_init');

?>

In the video, he mentions that by removing function update() the widget will still update...and in the video it does...but when I try so save text in the widget, it doesn't work...it just comes back blank.

I have also tried adding function update() as shown below with no luck either:

function update($new_instance, $old_instance) {
        $instance = $old_instance;

        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['body'] = strip_tags( $new_instance['body'] );

        return $instance;
    }

Any suggestions for a beginning plugin developer why this isn't saving at all?

Other pertinent information Wordpress Build: 3.3.1

EDIT: Fixed an erroneous line of pasted code.

share|improve this question

closed as too localized by toscho Jan 3 at 23:21

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

So the reason that the form wasn't updating was because I neglected to echo the php in the name.

So name="<?php $this->get_field_name('title'); ?>" changed to name="<?php echo $this->get_field_name('title'); ?>"

and that solved my problem for both the title and body input portions.

I am now able to save and update to my hearts content!

share|improve this answer

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