Advertisement
Guest User

Eunoia Extra Meta Field

a guest
Mar 10th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.51 KB | None | 0 0
  1. $portfoliourl_1_metabox = array(
  2.     'id' => 'portfoliourl',
  3.     'title' => 'Portfolio URL',
  4.     'page' => array('portfolio'),
  5.     'context' => 'side',
  6.     'priority' => 'low',
  7.     'fields' => array(
  8.  
  9.  
  10.                 array(
  11.                     'name'          => 'Portfolio Meta URL',
  12.                     'desc'          => 'Enter a URL you want to use somewhere in the portfolio for a link',
  13.                     'id'                => 'nick_portfolio_url',
  14.                     'class'             => 'nick_portfolio_url',
  15.                     'type'          => 'text',
  16.                     'rich_editor'   => 0,
  17.                     'max'           => 0                ),
  18.                 )
  19. );
  20.  
  21. add_action('admin_menu', 'nick_add_portfoliourl_1_meta_box');
  22. function nick_add_portfoliourl_1_meta_box() {
  23.  
  24.     global $portfoliourl_1_metabox;
  25.  
  26.     foreach($portfoliourl_1_metabox['page'] as $page) {
  27.         add_meta_box($portfoliourl_1_metabox['id'], $portfoliourl_1_metabox['title'], 'nick_show_portfoliourl_1_box', $page, 'side', 'low', $portfoliourl_1_metabox);
  28.     }
  29. }
  30.  
  31. // function to show meta boxes
  32. function nick_show_portfoliourl_1_box() {
  33.     global $post;
  34.     global $portfoliourl_1_metabox;
  35.     global $nick_prefix;
  36.     global $wp_version;
  37.  
  38.     // Use nonce for verification
  39.     echo '<input type="hidden" name="nick_portfoliourl_1_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  40.  
  41.     echo '<table class="form-table">';
  42.  
  43.     foreach ($portfoliourl_1_metabox['fields'] as $field) {
  44.         // get current post meta data
  45.  
  46.         $meta = get_post_meta($post->ID, $field['id'], true);
  47.  
  48.         echo '<tr>',
  49.                 '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
  50.                 '<td class="nick_field_type_' . str_replace(' ', '_', $field['type']) . '">';
  51.         switch ($field['type']) {
  52.             case 'text':
  53.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /><br/>', '', stripslashes($field['desc']);
  54.                 break;
  55.            
  56.            
  57.            
  58.  
  59.            
  60.         }
  61.         echo     '<td>',
  62.             '</tr>';
  63.     }
  64.  
  65.     echo '</table>';
  66. }
  67.  
  68. // Save data from meta box
  69. add_action('save_post', 'nick_portfoliourl_1_save');
  70. function nick_portfoliourl_1_save($post_id) {
  71.     global $post;
  72.     global $portfoliourl_1_metabox;
  73.  
  74.     // verify nonce
  75.     if ( ! isset( $_POST['nick_portfoliourl_1_meta_box_nonce'] ) || ! wp_verify_nonce($_POST['nick_portfoliourl_1_meta_box_nonce'], basename(__FILE__))) {
  76.         return $post_id;
  77.     }
  78.  
  79.     // check autosave
  80.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  81.         return $post_id;
  82.     }
  83.  
  84.     // check permissions
  85.     if ('page' == $_POST['post_type']) {
  86.         if (!current_user_can('edit_page', $post_id)) {
  87.             return $post_id;
  88.         }
  89.     } elseif (!current_user_can('edit_post', $post_id)) {
  90.         return $post_id;
  91.     }
  92.  
  93.     foreach ($portfoliourl_1_metabox['fields'] as $field) {
  94.  
  95.         $old = get_post_meta($post_id, $field['id'], true);
  96.         $new = $_POST[$field['id']];
  97.  
  98.         if ($new && $new != $old) {
  99.             if($field['type'] == 'date') {
  100.                 $new = nick_format_date($new);
  101.                 update_post_meta($post_id, $field['id'], $new);
  102.             } else {
  103.                 if(is_string($new)) {
  104.                     $new = $new;
  105.                 }
  106.                 update_post_meta($post_id, $field['id'], $new);
  107.  
  108.  
  109.             }
  110.         } elseif ('' == $new && $old) {
  111.             delete_post_meta($post_id, $field['id'], $old);
  112.         }
  113.     }
  114. }
  115.  
  116.  
  117. function nick_export_ui_scripts() {
  118.  
  119.     global $nick_options, $post;
  120.     ?>
  121.     <script type="text/javascript">
  122.             jQuery(document).ready(function($)
  123.             {
  124.  
  125.                 if($('.form-table .nick_upload_field').length > 0 ) {
  126.                     // Media Uploader
  127.                     window.formfield = '';
  128.  
  129.                     $('.nick_upload_image_button').live('click', function() {
  130.                     window.formfield = $('.nick_upload_field',$(this).parent());
  131.                         tb_show('', 'media-upload.php?type=file&post_id=<?php echo $post->ID; ?>&TB_iframe=true');
  132.                                         return false;
  133.                         });
  134.  
  135.                         window.original_send_to_editor = window.send_to_editor;
  136.                         window.send_to_editor = function(html) {
  137.                             if (window.formfield) {
  138.                                 imgurl = $('a','<div>'+html+'</div>').attr('href');
  139.                                 window.formfield.val(imgurl);
  140.                                 tb_remove();
  141.                             }
  142.                             else {
  143.                                 window.original_send_to_editor(html);
  144.                             }
  145.                             window.formfield = '';
  146.                             window.imagefield = false;
  147.                         }
  148.                 }
  149.  
  150.             });
  151.       </script>
  152.     <?php
  153. }
  154.  
  155. function nick_export_datepicker_ui_scripts() {
  156.     global $nick_base_dir;
  157.     wp_enqueue_script('jquery-ui-datepicker');
  158.     wp_enqueue_script('jquery-ui-slider');
  159. }
  160. function nick_export_datepicker_ui_styles() {
  161.     global $nick_base_dir;
  162.     wp_enqueue_style('jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css', false, '1.8', 'all');
  163. }
  164.  
  165. // these are for newest versions of WP
  166. add_action('admin_print_scripts-post.php', 'nick_export_datepicker_ui_scripts');
  167. add_action('admin_print_scripts-edit.php', 'nick_export_datepicker_ui_scripts');
  168. add_action('admin_print_scripts-post-new.php', 'nick_export_datepicker_ui_scripts');
  169. add_action('admin_print_styles-post.php', 'nick_export_datepicker_ui_styles');
  170. add_action('admin_print_styles-edit.php', 'nick_export_datepicker_ui_styles');
  171. add_action('admin_print_styles-post-new.php', 'nick_export_datepicker_ui_styles');
  172.  
  173. if ((isset($_GET['post']) && (isset($_GET['action']) && $_GET['action'] == 'edit') ) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php')))
  174. {
  175.     add_action('admin_head', 'nick_export_ui_scripts');
  176. }
  177.  
  178. // converts a time stamp to date string for meta fields
  179. if(!function_exists('nick_timestamp_to_date')) {
  180.     function nick_timestamp_to_date($date) {
  181.  
  182.         return date('m/d/Y', $date);
  183.     }
  184. }
  185. if(!function_exists('nick_format_date')) {
  186.     function nick_format_date($date) {
  187.  
  188.         $date = strtotime($date);
  189.  
  190.         return $date;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement