Advertisement
rakeshr

custom metabox code

Sep 12th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php define('MY_WORDPRESS_FOLDER',$_SERVER['DOCUMENT_ROOT']);
  2. define('MY_THEME_FOLDER',str_replace('\\','/',dirname(__FILE__)));
  3. define('MY_THEME_PATH','/' . substr(MY_THEME_FOLDER,stripos(MY_THEME_FOLDER,'wp-content')));
  4.  
  5. add_action('admin_init','my_meta_init');
  6.  
  7. function my_meta_init()
  8. {
  9.     // review the function reference for parameter details
  10.     // http://codex.wordpress.org/Function_Reference/wp_enqueue_script
  11.     // http://codex.wordpress.org/Function_Reference/wp_enqueue_style
  12.  
  13.     //wp_enqueue_script('my_meta_js', MY_THEME_PATH . '/custom/meta.js', array('jquery'));
  14.     wp_enqueue_style('my_meta_css', MY_THEME_PATH . '/custom/meta.css');
  15.  
  16.     // review the function reference for parameter details
  17.     // http://codex.wordpress.org/Function_Reference/add_meta_box
  18.  
  19.  
  20.     foreach (array('whatsnew') as $type)
  21.     {
  22.         add_meta_box('my_all_meta', 'New Products Details', 'whatsnew_setup', $type, 'normal', 'high');
  23.     }
  24.    
  25.     foreach (array('dealer') as $type)
  26.     {
  27.         add_meta_box('my_all_meta', 'Dealer Details', 'dealer_setup', $type, 'normal', 'high');
  28.     }
  29.  
  30.    
  31.     add_action('save_post','my_meta_save');
  32. }
  33.  
  34.  
  35.  
  36. function whatsnew_setup()
  37. {
  38.     global $post;
  39.  
  40.     // using an underscore, prevents the meta variable
  41.     // from showing up in the custom fields section
  42.     $meta = get_post_meta($post->ID,'_my_meta',TRUE);
  43.  
  44.     // instead of writing HTML here, lets do an include
  45.     include(MY_THEME_FOLDER . '/custom/whatsnew.php');
  46.  
  47.     // create a custom nonce for submit verification later
  48.     echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
  49. }
  50.  
  51. function dealer_setup()
  52. {
  53.     global $post;
  54.  
  55.     // using an underscore, prevents the meta variable
  56.     // from showing up in the custom fields section
  57.     $meta = get_post_meta($post->ID,'_my_meta',TRUE);
  58.  
  59.     // instead of writing HTML here, lets do an include
  60.     include(MY_THEME_FOLDER . '/custom/dealers.php');
  61.  
  62.     // create a custom nonce for submit verification later
  63.     echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
  64. }
  65.  
  66.  
  67.  
  68.  
  69. function my_meta_save($post_id)
  70. {
  71.     // authentication checks
  72.  
  73.     // make sure data came from our meta box
  74.     if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;
  75.  
  76.     // check user permissions
  77.     if ($_POST['post_type'] == 'whatsnew')
  78.         {
  79.         if (!current_user_can('edit_post', $post_id)) return $post_id;
  80.     }
  81.    
  82.     if ($_POST['post_type'] == 'dealer')
  83.         {
  84.         if (!current_user_can('edit_post', $post_id)) return $post_id;
  85.     }
  86.  
  87.  
  88.     // authentication passed, save data
  89.  
  90.     // var types
  91.     // single: _my_meta[var]
  92.     // array: _my_meta[var][]
  93.     // grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]
  94.  
  95.     $current_data = get_post_meta($post_id, '_my_meta', TRUE); 
  96.  
  97.     $new_data = $_POST['_my_meta'];
  98.  
  99.     my_meta_clean($new_data);
  100.    
  101.     if ($current_data)
  102.     {
  103.         if (is_null($new_data)) delete_post_meta($post_id,'_my_meta');
  104.         else update_post_meta($post_id,'_my_meta',$new_data);
  105.     }
  106.     elseif (!is_null($new_data))
  107.     {
  108.         add_post_meta($post_id,'_my_meta',$new_data,TRUE);
  109.     }
  110.  
  111.     return $post_id;
  112. }
  113.  
  114. function my_meta_clean(&$arr)
  115. {
  116.     if (is_array($arr))
  117.     {
  118.         foreach ($arr as $i => $v)
  119.         {
  120.             if (is_array($arr[$i]))
  121.             {
  122.                 my_meta_clean($arr[$i]);
  123.  
  124.                 if (!count($arr[$i]))
  125.                 {
  126.                     unset($arr[$i]);
  127.                 }
  128.             }
  129.             else
  130.             {
  131.                 if (trim($arr[$i]) == '')
  132.                 {
  133.                     unset($arr[$i]);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         if (!count($arr))
  139.         {
  140.             $arr = NULL;
  141.         }
  142.     }
  143. }
  144.  
  145.  
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement