Support » Plugins » Hacks » Checkbox meta value not saving

  • Hey guys, the text field is saving but the checkbox value isn’t.

    Can anyone please take a look, what’s wrong.
    Help’d be much entertained and appreciated.

    Thanks

    function ntp_create_meta_box(){
    	add_meta_box(
    		'ntp_meta_box',
    		'NTP Meta Box',
    		'show_ntp_meta_box',
    		'ntp_package',
    		'normal',
    		'high'
    		);
    }
    add_action('add_meta_boxes', 'ntp_create_meta_box');
    
    $pre = 'ntp_package_';
    $ntp_meta_fields = array(
    		array(
    			'label'		=> 'Package Price',
    			'desc'		=> 'Cost Per Package',
    			'id'		=> $pre.'price',
    			'type'		=> 'text',
    			),
    
    		array(
    			'label'		=> 'Featured',
    			'desc'		=> 'This is a Featured Package.',
    			'id'		=> $pre.'featured',
    			'type'		=> 'checkbox',
    			),
    
    		);
    
    function show_ntp_meta_box(){
    	global $ntp_meta_fields, $post;
    	echo '<input type="hidden" name="ntp_meta_box_nonce" value="'.wp_create_nonce(basename(_FILE_)).'"/>';
    
    	echo '<table class="ntp_meta_table">';
    
    	foreach($ntp_meta_fields as $field){
    		$meta = get_post_meta($post->ID, $field['id'], true);
    
    		echo	'<tr>
    				<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
    				</td>';
    
    				switch($field['type']){
    
    					case 'text':
    						echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30"/>
    							 <span class="meta_desc">'.$field['desc'].'</span>';
    							 break;
    
    					case 'checkbox':
    						echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked" ' : '','/>
    							<label for="'.$field['id'].'">'.$field['desc'].'</label>';
    							break;
    				}
    
    		echo	'</td></tr>';
    	}
    	echo '</table>';
    
    }	
    
    //------------save meta data------------
    
    function save_ntp_meta($post_id){
    	global $ntp_meta_fields;
    
    	if(!wp_verify_nonce($_POST['ntp_meta_box_nonce'], basename(_FILE_))) return;
    
    	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)	return;
    
    	if('page' == $_POST['post_type']){
    		if(!current_user_can('edit_page', $post_id))
    		return $post_id;
    	}	elseif (!current_user_can('edit_post', $post_id)){
    		return $post_id;
    	}	
    
    	foreach($ntp_meta_fields as $field){
    		$old = get_post_meta($post_id, $field['id'], true);
    		$new = isset($_POST[$field['id']]) ? $_POST[$field['id']] : null;
    
    		if($new && $new != $old){
    			update_post_meta($post_id, $field['id'], $new);
    		}elseif('' == $new && $old){
    			delete_post_meta($post_id, $field['id'], $old);
    		}
    
    	}	
    
    }	
    
    add_action('save_post', 'save_ntp_meta');
  • The topic ‘Checkbox meta value not saving’ is closed to new replies.