wordpress - Ajaxify WP Options - Problems with using jquery serialize and checkboxes -
i've been working on theme options panel wordpress. i've run bug when start using more 1 checkbox option.
my code uses jquery serialize form data , submit wp's ajax-url. add callback function , wp knows send data function have set save options db.
it works when check boxes on, hit save , no problem. values saved. boxes checked if try uncheck 1 or 2 of 3 , click save... on refresh boxes still checked. values still 'on' in db. think b/c jquery doesn't serialize unchecked checkboxes, aren't passed update_option array. since aren't in update_option array values keys stays same in db. hence, no change. strangely (to me atleast) if uncheck 3 of test checkboxes update properly.
so, looking work around update options correct values , remove checkbox values have been unchecked.
<?php add_action('admin_menu', 'test_add_theme_page'); function test_add_theme_page() { if ( isset( $_get['page'] ) && $_get['page'] == basename(__file__) ) { add_action('admin_head', 'test_theme_page_head'); } add_theme_page(__('test admin'), __('test admin'), 'edit_themes', basename(__file__), 'test_theme_page'); } function test_theme_page_head() { ?> <script type="text/javascript"> jquery(document).ready(function($) { jquery('form#test_form').submit(function() { var data = jquery(this).serialize(); alert(data); jquery.post(ajaxurl, data, function(response) { if(response == 1) { show_message(1); t = settimeout('fade_message()', 2000); } else { show_message(2); t = settimeout('fade_message()', 2000); } }); return false; }); }); function show_message(n) { if(n == 1) { jquery('#saved').html('<div id="message" class="updated fade"><p><strong><?php _e('options saved.'); ?></strong></p></div>').show(); } else { jquery('#saved').html('<div id="message" class="error fade"><p><strong><?php _e('options not saved.'); ?></strong></p></div>').show(); } } function fade_message() { jquery('#saved').fadeout(1000); cleartimeout(t); } </script> <?php } function test_theme_page() { ?> <div class="wrap"> <h2><?php _e('test admin'); ?></h2> <div id="saved"></div> <?php $options = get_option('test_theme'); echo "<br>"; print_r($options); echo"<br>"; ?> <form action="/" name="test_form" id="test_form"> text<input type="text" name="test_text" value="<?php echo $options['test_text']; ?>" /><br /> check1<input type="checkbox" name="test_check1" <?php echo ($options['test_check1'] == 'on') ? 'checked' : ''; ?> /><br /> check2<input type="checkbox" name="test_check2" <?php echo ($options['test_check2'] == 'on') ? 'checked' : ''; ?> /><br /> check3<input type="checkbox" name="test_check3" <?php echo ($options['test_check3'] == 'on') ? 'checked' : ''; ?> /><br /> <input type="hidden" name="action" value="test_theme_data_save" /> <input type="hidden" name="security" value="<?php echo wp_create_nonce('test-theme-data'); ?>" /> <input type="submit" value="submit" /> </form> </div> <?php } add_action('wp_ajax_test_theme_data_save', 'test_theme_save_ajax'); function test_theme_save_ajax() { check_ajax_referer('test-theme-data', 'security'); $data = $_post; unset($data['security'], $data['action']); if(!is_array(get_option('test_theme'))) { $options = array(); } else { $options = get_option('test_theme'); } if(!empty($data)) { $diff = array_diff($options, $data); $diff2 = array_diff($data, $options); $diff = array_merge($diff, $diff2); } else { $diff = array(); } if(!empty($diff)) { if(update_option('test_theme', $data)) { die('1'); } else { die('0'); } } else { die('1'); } }
you can see full code here http://pastebin.com/bchwsbi5
was apparently 1. over-complicating things , 2. not understanding update_option returns true on update , returns false if there no change , not on failure.
here's corrected test_theme_save_ajax() function:
function test_theme_save_ajax() { check_ajax_referer('test-theme-data', 'security'); $data = $_post; unset($data['security'], $data['action']); update_option('test_theme', $data); die('1'); }
Comments
Post a Comment