Advertisement
manchumahara

Custom field for custom post type

Aug 1st, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.39 KB | None | 0 0
  1. add_action("admin_init", "cb_add_extrametafields");
  2. function cb_add_extrametafields(){
  3.     //add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
  4.     add_meta_box( 'cbextrametafields', 'Extra Post Options', 'cb_add_extrametafields_html', 'product', 'normal', 'high');
  5.  
  6. }
  7.  
  8. function cb_add_extrametafields_html(){
  9.     global $post;
  10.     $custom = get_post_custom($post->ID);    
  11.    
  12.     $jobzipcode     = $custom["jobzipcode"][0]; //job location zipcode
  13.     $jnchild        = $custom["jnchild"][0]; //no. of children
  14.     $jrate1         =  $custom["jrate1"][0]; //rate start
  15.     $jrate2         =  $custom["jrate2"][0]; //rate end
  16.     $jrateb         =  $custom["jrateb"][0]; //per hour/ per week
  17.     $jstartdate     =  $custom["jstartdate"][0]; //per hour/ per week
  18.     $jstartdate     = convert_datetous($jstartdate); // yyyy-mm-dd  to mm-dd-yyyy
  19.     $appstatus      =  $custom["appstatus"][0]; //application status, open/close
  20.     if($jrateb == ''){$jrateb = 'hour';}
  21.     if($appstatus == ''){$appstatus = 'open';}
  22.    
  23.     // Use nonce for verification
  24.     wp_nonce_field( plugin_basename( __FILE__ ), 'cb_extrametafields' );
  25.     ?>
  26.     <style type="text/css">
  27.         .cblabel{
  28.             display: inline-block;
  29.             margin: 0 5px 5px 0;
  30.             text-align: left;
  31.             width: 150px;
  32.         }
  33.     </style>
  34.     <div class="detail">
  35.             <p><label class="cblabel">ZIP Code:</label><input size="20" name="jobzipcode" value="<?php echo $jobzipcode; ?>" /></p>
  36.             <p><label class="cblabel">No. Children:</label><input size="20" name="jnchild" value="<?php echo $jnchild; ?>" /></p>
  37.             <p>
  38.                 <label class="cblabel">Rate range (in whole dollars)::</label>From <input size="20" name="jrate1" value="<?php echo $jrate1; ?>" /> To <input size="20" name="jrate2" value="<?php echo $jrate2; ?>" /> per
  39.                 <select id="jrateb" name="jrateb" class="input">
  40.                     <option value="hour" <?php selected('hour',$jrateb); ?>>Hour</option>
  41.                     <option value="week" <?php selected('week',$jrateb); ?>>Week</option>
  42.                 </select>
  43.             </p>
  44.             <p><label class="cblabel">Job Start Date:</label><input size="20" name="jstartdate" value="<?php echo $jstartdate; ?>" />(Format: mm-dd-yyyy)</p>
  45.             <p>
  46.                 <label class="appstatus">Application Status: </label>
  47.                 <select id="appstatus" name="appstatus" class="input">
  48.                     <option value="open" <?php selected('open',$appstatus); ?>>Open</option>
  49.                     <option value="close" <?php selected('close',$appstatus); ?>>Close</option>
  50.                 </select>
  51.             </p>
  52.     </div>    
  53.     <?php
  54. }
  55.  
  56. function cb_update_extrametafields($post_id){
  57.     global $post;
  58.     // verify if this is an auto save routine.
  59.   // If it is our form has not been submitted, so we dont want to do anything
  60.   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  61.       return;
  62.  
  63.   // verify this came from the our screen and with proper authorization,
  64.   // because save_post can be triggered at other times
  65.  
  66.   if ( !wp_verify_nonce( $_POST['cb_extrametafields'], plugin_basename( __FILE__ ) ) )
  67.       return;
  68.  
  69.   // Check permissions
  70.   //var_dump($_POST['post_type']);
  71.   if ( 'product' == $_POST['post_type'] )
  72.   {
  73.     if ( !current_user_can( 'edit_post', $post->id ) )
  74.         return;
  75.   }
  76.   else  { return; }
  77.  
  78.   // OK, we're authenticated: we need to find and save the data
  79.   //$post_id, $meta_key, $meta_value, $prev_value = ''
  80.   update_post_meta($post->ID, "jobzipcode", $_POST["jobzipcode"]);
  81.   $latlng = zipcodes_latlng($_POST["jobzipcode"]);
  82.   if($latlng != false){
  83.         update_post_meta($post->ID, "latitude", $latlng->latitude);
  84.         update_post_meta($post->ID, "longitude", $latlng->longitude);
  85.   }
  86.   update_post_meta($post->ID, "jnchild", $_POST["jnchild"]);
  87.   update_post_meta($post->ID, "jrate1", $_POST["jrate1"]);
  88.   update_post_meta($post->ID, "jrate2", $_POST["jrate2"]);
  89.   update_post_meta($post->ID, "jrateb", $_POST["jrateb"]);
  90.   $jstartdate = $_POST["jstartdate"];
  91.   $jstartdate = convert_ustodate($jstartdate);
  92.   update_post_meta($post->ID, "jstartdate", $jstartdate );
  93.   update_post_meta($post->ID, "appstatus", $_POST["appstatus"]);
  94. }
  95.  
  96. add_action("admin_init", "cb_add_extrametafields");
  97. add_action('save_post', 'cb_update_extrametafields');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement