Support » Plugins » Hacks » Widget fails to save settings

  • Hi all

    I know I’m doing something wrong, but I seem to stupid to see it…

    I created a widget (based off of WP_Widget_Meta) and it won’t save the configuration when I change it.

    If anyone has any pointers, please let me know. The code is below…

    Cheers

    Remo

    <?php
    /**
     * Plugin Name: Murkwood Widget
     * Plugin URI: http://www.murkwoodfilm.com
     * Description: A widget for Murkwood.
     * Version: 0.2
     * Author: Remo Pini
     * Author URI: http://www.pini.org
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     */
    
    // Add function to widgets_init that'll load our widget.
    add_action( 'widgets_init', 'murkwood_load_widgets' );
    
    // Register our widget.
    function murkwood_load_widgets() {
    	register_widget('Widget_Murkwood');
    }
    
    // Murkwood Widget class.
    class Widget_Murkwood extends WP_Widget
    {
    	// Widget setup.
    	function __construct()
    	{
    		$widget_ops = array( 'classname' => 'widget_murkwood', 'description' => __('A widget for Murkwood.') );
    		parent::__construct('murkwood', __('Murkwood'), $widget_ops);
    	}
    
    	function update( $new_instance, $old_instance )
    	{
    		$instance = $old_instance;
    		$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'showentryrss' => 1, 'showcommentrss' => 1, 'showwplink' => 0, 'showmklink' => 1) );
    		$instance['title']          = strip_tags($new_instance['title']);
    		$instance['showentryrss']   = $new_instance['showentryrss']   ? 1 : 0;
    		$instance['showcommentrss'] = $new_instance['showcommentrss'] ? 1 : 0;
    		$instance['showwplink']     = $new_instance['showwplink']     ? 1 : 0;
    		$instance['showmklink']     = $new_instance['showmklink']     ? 1 : 0;
    		return $instance;
    	}
    
    	function form()
    	{
    		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'showentryrss' => 1, 'showcommentrss' => 1, 'showwplink' => 0, 'showmklink' => 1) );
    		$title = strip_tags($instance['title']);
    		$showentryrss   = $instance['showentryrss']   ? 'checked="checked"' : '';
    		$showcommentrss = $instance['showcommentrss'] ? 'checked="checked"' : '';
    		$showwplink     = $instance['showwplink']     ? 'checked="checked"' : '';
    		$showmklink     = $instance['showmklink']     ? 'checked="checked"' : '';
    ?>
    		<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>
    			<input class="checkbox" type="checkbox" <?php echo $showentryrss;   ?> id="<?php echo $this->get_field_id('showentryrss'  ); ?>" name="<?php echo $this->get_field_name('showentryrss'  ); ?>" /> <label for="<?php echo $this->get_field_id('showentryrss'  ); ?>"><?php _e('Show Entry RSS Link'  ); ?></label><br/>
    			<input class="checkbox" type="checkbox" <?php echo $showcommentrss; ?> id="<?php echo $this->get_field_id('showcommentrss'); ?>" name="<?php echo $this->get_field_name('showcommentrss'); ?>" /> <label for="<?php echo $this->get_field_id('showcommentrss'); ?>"><?php _e('Show Comment RSS Link'); ?></label><br/>
    			<input class="checkbox" type="checkbox" <?php echo $showwplink;     ?> id="<?php echo $this->get_field_id('showwplink'    ); ?>" name="<?php echo $this->get_field_name('showwplink'    ); ?>" /> <label for="<?php echo $this->get_field_id('showwplink'    ); ?>"><?php _e('Show WordPress Link'  ); ?></label><br/>
    			<input class="checkbox" type="checkbox" <?php echo $showmklink;     ?> id="<?php echo $this->get_field_id('showmklink'    ); ?>" name="<?php echo $this->get_field_name('showmklink'    ); ?>" /> <label for="<?php echo $this->get_field_id('showmklink'    ); ?>"><?php _e('Show MURKWOOD Link'   ); ?></label>
    		</p>
    <?php
    	}
    
    	function widget( $args, $instance )
    	{
    		extract($args);
    		$showentryrss   = !empty($instance['showentryrss']  )? 1 : 0;
    		$showcommentrss = !empty($instance['showcommentrss'])? 1 : 0;
    		$showwplink     = !empty($instance['showwplink']    )? 1 : 0;
    		$showmklink     = !empty($instance['showmklink']    )? 1 : 0;
    		$title = apply_filters('widget_title', empty($instance['title']) ? __('Murkwood') : $instance['title'], $instance, $this->id_base);
    
    		echo $before_widget;
    		if ( $title )
    			echo $before_title . $title . $after_title;
    ?>
    		<ul>
    			<?php wp_register(); ?>
    			<li><?php wp_loginout(); ?></li>
    			<?php if($showentryrss) { ?>
    				<li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
    			<?php } ?>
    			<?php if($showcommentrss) { ?>
    				<li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
    			<?php } ?>
    			<?php if($showwplink) { ?>
    				<li><a href="<?php esc_attr_e('http://wordpress.org/'); ?>" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>"><?php _e( 'WordPress.org' ); ?></a></li>
    			<?php } ?>
    			<?php if($showmklink) { ?>
    				<li><a href="<?php esc_attr_e('http://www.murkwoodfilm.com/'); ?>" title="<?php echo esc_attr(__('MURKWOOD')); ?>"><?php _e( 'Murkwoodfilm.com' ); ?></a></li>
    			<?php } ?>
    			<?php wp_meta(); ?>
    		</ul>
    <?php
    		echo $after_widget;
    	}
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Try:
    function form( $instance )

    The settings are probably saved, just not displayed back to you. Mind you, I didn’t look too close, but a form display function with no parameters or globals struck me as odd.

    Thread Starter remopini

    (@remopini)

    Hm… I failed to see that when going over the whole thing last night. I’ve added the “$instance”, but that alone didn’t seem to change anything…

    Thread Starter remopini

    (@remopini)

    One interesting observation:

    The TITLE seems to be updated/saved just fine. The checkboxes aren’t. So the error must be somewhere in there…

    Thread Starter remopini

    (@remopini)

    I also checked the values of the instances in the update function:

    the initial $new_instance that is passed to the update function only contains the “title” array entry, the others aren’t there… sooo there must be something wrong with the form function, I guess, but what?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Widget fails to save settings’ is closed to new replies.