[wp-hackers] Need help with: coding new Plugin options

Daiv Mowbray daiv at daivmowbray.com
Wed Nov 5 09:58:59 GMT 2008


Hello list,

Hoping someone can help me with this.
I'm building my first plugin, and I'm lost in a deep dark hole at the  
plugin options stage.

I am able to create the options page, and preload default options into  
the data base as an array.
I am able to load default options into the form.
I can save the form , but it saves all items as blank.
Plus, without making changes to the options, I can't figure out how to  
pull the options and assign each
item of the array to a variable to pass to my java  script.

I've included bellow, a reduced version of what I think are the  
primary elements.
I'm sure I'm missing the boat here,
It's just that I'm not sure weather it should be a canoe or a speedboat.
Thanx for your time and assistance.


/**********************Functions loading defaults into options  
table******************************/
This bellow is called in the init function:
$this->defaultAdminOptions = $this->setDefaultAdminOptions();

function setDefaultAdminOptions() {
		$defaultAdminOptions = array("load_moo" => "true",
				"css_load" => "true");
				
		$defaultOptions = get_option($this->AdminOptionsName);
		if (!empty($defaultOptions)) {
			foreach ($defaultOptions as $key => $option) {
				$defaultAdminOptions[$key] = $option;
			}
		}
		update_option($this->AdminOptionsName, $defaultAdminOptions);
		return $defaultAdminOptions;
		
	}
		
		/**
		* Saves the admin options to the database.
		*/
		function savedefaultAdminOptions(){
			update_option($this->AdminOptionsName, $this->defaultAdminOptions);
		}
	
		/**
		* Removes user set options from data base upon deactivation  
(triggered with the register_deactivation_hook)
		*/
		function options_deactivation(){
			delete_option($AdminOptionsName);
			
		}


/**********************Trying to update options on the form  
(reduced)******************************/
if ( !current_user_can('manage_options') ) {
		die( __( 'ACCESS DENIED: Your don\'t have permission to do this.',  
'superslider-menu' ) );
		}
		elseif ($_POST['action'] && $_POST['action'] ==  
'update_ssm_options' ) {
		
					$ssmNewOptions = $this->savedefaultAdminOptions();

					// Did this $_POST come from our form?
					check_admin_referer('ssm_options');
					
					if (isset($_POST['update_ssm_options'])) {
						if (isset($_POST['op_load_moo'])) {
							$ssmNewOptions['load_moo'] = $_POST['op_load_moo'];
						}
		update_option($this->AdminOptionsName, $ssmNewOptions);

// If we've updated settings, show a message
echo '<div id="message" class="updated fade"><p><strong>' .  
__( 'SuperSlider-Menu Options saved.', 'superslider-menu' ) . '</ 
strong></p></div>';
		
		}
	}

/********************** The options page form (reduced):  
******************************/

<form name="ssm_options" method="post" action=""><!--<?php echo  
$_SERVER['REQUEST_URI'] ?> -->
<?php if ( function_exists('wp_nonce_field') )
		wp_nonce_field('ssm_options'); echo "\n"; ?>
   <div style=""> <h2 style="display:inline; position: relative;"><? 
php _e('Plugin Options'); ?></h2></div>
<fieldset>
<legend><b><?php _e('Header Options'); ?>:</b></legend>
     	<label for="op_load_moo"><input type="checkbox" name="load_moo"
     	<?php if($ssmNewOptions['load_moo'] == "true") echo $checked; ?>  
id="op_load_moo" />
     	<?php _e('Load Mootools 1.2 into your theme header.'); ?></label>
     	<br /><small><?php _e(' Your theme, or another plugin may  
already load mootools.'); ?></small>
   </fieldset>
	<p class="submit">
		<input type="submit" name="update_ss_menu" id= "update_ss_menu"   
value="<?php _e('Update options'); ?> &raquo;" />
		<input type="hidden" name="action" value="update_ss_menu" />
  	</p>
  </form>

/********************** End code sample ******************************/


----------
Daiv Mowbray
daiv at daivmowbray.com
----------




More information about the wp-hackers mailing list