1 Replies - 12636 Views - Last Post: 01 June 2012 - 09:52 AM

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Checkbox state won't save in wordpress widget

Posted 31 May 2012 - 11:38 AM

I'm working on a WordPress widget right now that has dynamic checkboxes that the user can add. The checkboxes are used to create different categories for the widget. The problem I'm having is saving the state of these checkboxes (whether they're checked or not). Current I have it setup so the label is saved in the WordPress database, but when I try to save the state it just doesn't work.

Here is the full widget class:
<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

/*
	Plugin Name: Image Gallery Uploader
	Plugin URI: https://github.com/kschat
	Description: This plugin allows visitors of your WordPress site to upload images to a gallery.
	Version: 0.1
	License: GPL2
*/

include_once 'Debug.php';

define('PLUGIN_PATH', plugin_dir_url(__FILE__));

class UploaderWidget extends WP_Widget {
	
	// Constructor
	function __construct() {
		$widget_ops = array(
		'classname' => 'image-uploader',
		'description' => 'A widget used to upload photos to a gallery.');
		
		$control_ops = array('width'=> '200', 'height'=> '350', 'id_base' => 'image-uploader-id');
		
		parent::__construct('image-uploader-id', 'Uploader Widget', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance) {
		extract($args);
		
		$title = apply_filters('widget_title', $instance['title']);
		
		echo $before_widget;
		
		if($title) {
			echo $before_title . $title . $after_title;
		}
		?>
		<img src="<?php echo PLUGIN_PATH;?>images/defaultImage.gif"><br />
		<p>
		<label for="upload-button" style="color:black;">Upload new Image:</label>
		<input type="file" name="upload-button" />
		</p>
		<?php
		
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['imgDir'] = $new_instance['imgDir'];
		//$instance['categories'][0]['selected'] = 1;
		//$instance['categories'][0]['label'] = '';
		//$instance['newCategory'] = '';
		
		
		
		$instance['categories'] = $this->parseAndCombineCategories((get_option('UploaderWidget_categories')), $new_instance['newCategory']);
		
		for($i=0; $i<sizeof($instance['categories']); $i++) {
			$instance['categories'][$i]['selected'] = (int)$new_instance['categories'][$i]['selected'];
		}
		
		//$instance['categories'][0]['selected'] = 1;
		//$instance['categories'][0]['label'] = 'label';
		
		update_option('UploaderWidget_categories', $instance['categories']);
		
		return $instance;
	}
	
	function form($instance) {
		$defaults = array('title' 		=> 'Uploader Widget', 
						  'imgDir' 		=> PLUGIN_PATH.'images/', 
						  'categories' 	=> array(0 => array('selected'	 => 0, 
													   		'label'		 => 'option1'),
												 1 => array('selected' 	 => 0, 
												 			'label' 	 => 'option2'),
												 2 => array('selected'	 => 0, 
												 	   		'label' 	 => 'option3')));
		
		$instance = wp_parse_args((array)$instance, $defaults);
		//$instance['categories'] = null;
		//$instance['newCategory'] = null;
		?>
		<p>
			<label for="<?php echo $this->get_field_id('title');?>">Title:</label><br />
			<input type= "text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>" 
				   value="<?php echo $instance['title'];?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('imgDir');?>">Saved Images Directory:</label>
			<input type="text" id="<?php echo $this->get_field_id('imgDir');?>" name="<?php echo $this->get_field_name('imgDir');?>" 
					value="<?php echo $instance['imgDir'];?>" readonly="readonly" />
			<input type="file" />
		</p>
		
		<p>
			Categories:<br />
			<?php 
			$this->displayCategories($instance['categories']); 
			$this->displayNewCategoriesInput('newCategory'); 
			?>
		</p>
		<?php
	}
	
	function parseAndCombineCategories($categories, $newCategories) {
		//unset($categories);
		if(trim($newCategories) != "") {
			unset($categories['NoCategories']);
			$newCategories = split(',', $newCategories);
			$newSize = sizeof($categories) + sizeof($newCategories);
			$oldSize = sizeof($categories);
			
			for($i=sizeof($categories); $i<$newSize; $i++) {
				$categories[$i]['selected'] = 0;
				$categories[$i]['label'] = $newCategories[$i - $oldSize];
			}
		}
		else if(empty($categories)) {
			return $categories = array('NoCategories' => '<b>No categories have been made yet.</b>');
		}
		return (array)$categories;
	}
	
	function displayCategories($categories) {
		if(array_key_exists('NoCategories', $categories)) {
			?> <p> <?php
			echo $categories['NoCategories'];
			?> </p> <?php
			return;
		}
		?> <table class="form-table" id="image-gallery-form-table"> <?php
		for($i=0; $i<sizeof($categories); $i++) {
			echo ($i % 2 == 0) ? "<tr><td>": "<td>"; ?>
			<input class="checkboxes" type="checkbox" <?php checked((int)$categories[$i]['selected'], 1); ?> id="<?php echo $this->get_field_id($categories[$i]['label']); ?>" 
				   name="<?php echo $this->get_field_name($categories[$i]['label']);?>" />
				<label for="<?php echo $this->get_field_id($categories[$i]['label']); ?>"><?php echo $categories[$i]['label']; ?></label>
				<?php echo ($i % 2 == 0) ? "</td>": "</td></tr>";
		}
		?> </table> <?php echo Debug::printArray($categories, false);
	}
	
	function displayNewCategoriesInput($field) {
		?>
		<label for="<?php echo $this->get_field_id($field); ?>">Add a new category 
			(seperate names with a ',' to add multiple categories):</label>
			<input type="text" name="<?php echo $this->get_field_name($field); ?>" 
				   id="<?php echo $this->get_field_id($field); ?>" />
		<?php
	}
}

function registerUploaderWidget() {
	register_widget('UploaderWidget');
}

add_action('widgets_init', 'registerUploaderWidget');
?>




It should be saving the state in the update method, but for some reason it's not. I tried placing the for loop both before and after the call to parseAndCombineCategories() but it didn't make a difference. If I hard code the option (i.e. $instance['categories'][0]['selected'] = 1) it works, but if I try to set instance equal to new_instance like I do in the for loop nothing is saved in the selected node of the array. Something I actually noticed too is if there was already a 0 saved in the selected node and it runs through that loop 0 is no longer there, it's replaced with nothing.

This post has been edited by giggly kisses: 31 May 2012 - 12:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox state won't save in wordpress widget

#2 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Checkbox state won't save in wordpress widget

Posted 01 June 2012 - 09:52 AM

Found the solution, going to post it for anyone who might have the same problem and find this thread via google. After much tinkering and 'hacking' I found out why it wasn't saving and how to fix it. It wasn't saving because the values were saving to the post variable with a key equal to the value that is stored in my categories variable label node (worded it oddly, sorry ha). Anyway I did a 'quick fix' to get it to work so I have something to show at a meeting I have today. The fix was setting the name parameter to this:

name="<?php echo "widget-image-uploader-id[2][categories][" . $i . "][selected]"; ?>"


My long term fix is to create a categories class and make a list of that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1