Advertisement
Guest User

Serialized Options- Trouble with Checkboxes

a guest
Jan 25th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('admin_menu', 'test_add_theme_page');
  4.  
  5. function test_add_theme_page() {
  6.     if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {
  7.  
  8.         add_action('admin_head', 'test_theme_page_head');
  9.     }
  10.     add_theme_page(__('Test Admin'), __('Test Admin'), 'edit_themes', basename(__FILE__), 'test_theme_page');
  11. }
  12.  
  13.  
  14.  
  15.  
  16. function test_theme_page_head() {
  17. ?>
  18.  
  19.     <script type="text/javascript">
  20.     jQuery(document).ready(function($) {
  21.  
  22.       jQuery('form#test_form').submit(function() {
  23.           var data = jQuery(this).serialize();
  24.          
  25.           alert(data);
  26.          
  27.           jQuery.post(ajaxurl, data, function(response) {
  28.               if(response == 1) {
  29.                   show_message(1);
  30.                   t = setTimeout('fade_message()', 2000);
  31.               } else {
  32.                   show_message(2);
  33.                   t = setTimeout('fade_message()', 2000);
  34.               }
  35.           });
  36.           return false;
  37.       });
  38.  
  39.     });
  40.  
  41.     function show_message(n) {
  42.         if(n == 1) {
  43.             jQuery('#saved').html('<div id="message" class="updated fade"><p><strong><?php _e('Options saved.'); ?></strong></p></div>').show();
  44.         } else {
  45.             jQuery('#saved').html('<div id="message" class="error fade"><p><strong><?php _e('Options could not be saved.'); ?></strong></p></div>').show();
  46.         }
  47.     }
  48.  
  49.     function fade_message() {
  50.         jQuery('#saved').fadeOut(1000);
  51.         clearTimeout(t);
  52.     }
  53.     </script>
  54.  
  55. <?php
  56. }
  57.  
  58.  
  59.  
  60.  
  61. function test_theme_page() {
  62. ?>
  63.  
  64. <div class="wrap">
  65.  
  66.     <h2><?php _e('Test Admin'); ?></h2>
  67.  
  68.     <div id="saved"></div>
  69.     <?php $options = get_option('test_theme');
  70.    
  71.     echo "<br>";
  72.     print_r($options);
  73.     echo"<br>";
  74.     ?>
  75.     <form action="/" name="test_form" id="test_form">
  76.         Text<input type="text" name="test_text" value="<?php echo $options['test_text']; ?>" /><br />
  77.         Check1<input type="checkbox" name="test_check1" <?php echo ($options['test_check1'] == 'on') ? 'checked' : ''; ?> /><br />
  78.         Check2<input type="checkbox" name="test_check2" <?php echo ($options['test_check2'] == 'on') ? 'checked' : ''; ?> /><br />
  79.         Check3<input type="checkbox" name="test_check3" <?php echo ($options['test_check3'] == 'on') ? 'checked' : ''; ?> /><br />
  80.         <input type="hidden" name="action" value="test_theme_data_save" />
  81.         <input type="hidden" name="security" value="<?php echo wp_create_nonce('test-theme-data'); ?>" />
  82.         <input type="submit" value="Submit" />
  83.     </form>
  84.  
  85. </div>
  86.  
  87. <?php
  88. }
  89.  
  90.  
  91.  
  92.  
  93. add_action('wp_ajax_test_theme_data_save', 'test_theme_save_ajax');
  94.  
  95. function test_theme_save_ajax() {
  96.  
  97.     check_ajax_referer('test-theme-data', 'security');
  98.  
  99.     $data = $_POST;
  100.     unset($data['security'], $data['action']);
  101.  
  102.     if(!is_array(get_option('test_theme'))) {
  103.         $options = array();
  104.     } else {
  105.         $options = get_option('test_theme');
  106.     }
  107.  
  108.     if(!empty($data)) {
  109.         $diff = array_diff($options, $data);
  110.         $diff2 = array_diff($data, $options);
  111.         $diff = array_merge($diff, $diff2);
  112.     } else {
  113.         $diff = array();
  114.     }
  115.  
  116.     if(!empty($diff)) {
  117.         if(update_option('test_theme', $data)) {
  118.             die('1');
  119.         } else {
  120.             die('0');
  121.         }
  122.     } else {
  123.         die('1');
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement