1. $portfolioextrafields_1_metabox = array(
  2.     'id' => 'portfolioextrafields',
  3.     'title' => 'Portfolio Extra Fields',
  4.     'page' => array('portfolio'),
  5.     'context' => 'normal',
  6.     'priority' => 'high',
  7.     'fields' => array(
  8.  
  9.  
  10.                 array(
  11.                     'name'          => 'Employee Name',
  12.                     'desc'          => 'Enter the name of the employee',
  13.                     'id'                => 'nick_empname',
  14.                     'class'             => 'nick_empname',
  15.                     'type'          => 'text',
  16.                     'rich_editor'   => 0,
  17.                     'max'           => 0                ),
  18.            
  19.                 array(
  20.                     'name'          => 'Employee Lines',
  21.                     'desc'          => 'Enter the lines separated by <br/>',
  22.                     'id'                => 'nick_empline',
  23.                     'class'             => 'nick_empline',
  24.                     'type'          => 'text',
  25.                     'rich_editor'   => 0,
  26.                     'max'           => 0                ),
  27.                 )
  28. );
  29.  
  30. add_action('admin_menu', 'nick_add_portfolioextrafields_1_meta_box');
  31. function nick_add_portfolioextrafields_1_meta_box() {
  32.  
  33.     global $portfolioextrafields_1_metabox;
  34.  
  35.     foreach($portfolioextrafields_1_metabox['page'] as $page) {
  36.         add_meta_box($portfolioextrafields_1_metabox['id'], $portfolioextrafields_1_metabox['title'], 'nick_show_portfolioextrafields_1_box', $page, 'normal', 'high', $portfolioextrafields_1_metabox);
  37.     }
  38. }
  39.  
  40. // function to show meta boxes
  41. function nick_show_portfolioextrafields_1_box() {
  42.     global $post;
  43.     global $portfolioextrafields_1_metabox;
  44.     global $nick_prefix;
  45.     global $wp_version;
  46.  
  47.     // Use nonce for verification
  48.     echo '<input type="hidden" name="nick_portfolioextrafields_1_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  49.  
  50.     echo '<table class="form-table">';
  51.  
  52.     foreach ($portfolioextrafields_1_metabox['fields'] as $field) {
  53.         // get current post meta data
  54.  
  55.         $meta = get_post_meta($post->ID, $field['id'], true);
  56.  
  57.         echo '<tr>',
  58.                 '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
  59.                 '<td class="nick_field_type_' . str_replace(' ', '_', $field['type']) . '">';
  60.         switch ($field['type']) {
  61.             case 'text':
  62.             if (!isset($field['std'])) {$ttemp_string = "";} else {$ttemp_string = $field['std'];}
  63.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $ttemp_string, '" size="30" style="width:97%" /><br/>', '', stripslashes($field['desc']);
  64.                 break;
  65.        
  66.  
  67.            
  68.         }
  69.         echo     '<td>',
  70.             '</tr>';
  71.     }
  72.  
  73.     echo '</table>';
  74. }
  75.  
  76. // Save data from meta box
  77. add_action('save_post', 'nick_portfolioextrafields_1_save');
  78. function nick_portfolioextrafields_1_save($post_id) {
  79.     global $post;
  80.     global $portfolioextrafields_1_metabox;
  81.  
  82.     // verify nonce
  83.     if ( ! isset( $_POST['nick_portfolioextrafields_1_meta_box_nonce'] ) || ! wp_verify_nonce($_POST['nick_portfolioextrafields_1_meta_box_nonce'], basename(__FILE__))) {
  84.         return $post_id;
  85.     }
  86.  
  87.     // check autosave
  88.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  89.         return $post_id;
  90.     }
  91.  
  92.     // check permissions
  93.     if ('page' == $_POST['post_type']) {
  94.         if (!current_user_can('edit_page', $post_id)) {
  95.             return $post_id;
  96.         }
  97.     } elseif (!current_user_can('edit_post', $post_id)) {
  98.         return $post_id;
  99.     }
  100.  
  101.     foreach ($portfolioextrafields_1_metabox['fields'] as $field) {
  102.  
  103.         $old = get_post_meta($post_id, $field['id'], true);
  104.         $new = $_POST[$field['id']];
  105.  
  106.         if ($new && $new != $old) {
  107.             if($field['type'] == 'date') {
  108.                 $new = nick_format_date($new);
  109.                 update_post_meta($post_id, $field['id'], $new);
  110.             } else {
  111.                 if(is_string($new)) {
  112.                     $new = $new;
  113.                 }
  114.                 update_post_meta($post_id, $field['id'], $new);
  115.  
  116.  
  117.             }
  118.         } elseif ('' == $new && $old) {
  119.             delete_post_meta($post_id, $field['id'], $old);
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. function nick_export_ui_scripts() {
  126.  
  127.     global $nick_options, $post;
  128.     ?>
  129.     <script type="text/javascript">
  130.             jQuery(document).ready(function($)
  131.             {
  132.  
  133.                
  134.  
  135.                 // add new repeatable field
  136.                 $(".nick_add_new_field").on('click', function() {
  137.                     var field = $(this).closest('td').find("div.nick_repeatable_wrapper:last").clone(true);
  138.                     var fieldLocation = $(this).closest('td').find('div.nick_repeatable_wrapper:last');
  139.  
  140.                     // set the new field val to blank
  141.                     $('input', field).val("");
  142.                     field.insertAfter(fieldLocation, $(this).closest('td'));
  143.  
  144.                     return false;
  145.                 });
  146.  
  147.                
  148.  
  149.                 // remove repeatable field
  150.                 $('.nick_remove_repeatable').on('click', function(e) {
  151.                     e.preventDefault();
  152.                     var field = $(this).parent();
  153.                     $('input', field).val("");
  154.                     field.remove();
  155.                     return false;
  156.                 });
  157.  
  158.             });
  159.       </script>
  160.     <?php
  161. }