SHOW:
|
|
- or go back to the newest paste.
1 | $authorofsermon_1_metabox = array( | |
2 | 'id' => 'authorofsermon', | |
3 | 'title' => 'Author of Sermon', | |
4 | 'page' => array('sermon'), | |
5 | 'context' => 'normal', | |
6 | 'priority' => 'high', | |
7 | 'fields' => array( | |
8 | ||
9 | ||
10 | array( | |
11 | - | 'name' => 'Author's name', |
11 | + | 'name' => 'Name of Author', |
12 | - | 'desc' => 'Name of the person who is conducting the sermon.', |
12 | + | 'desc' => 'Name of the person who authored the sermon.', |
13 | 'id' => 'nick_field-sermon-author', | |
14 | 'class' => 'nick_field-sermon-author', | |
15 | 'type' => 'text', | |
16 | 'rich_editor' => 0, | |
17 | 'max' => 0 | |
18 | ), | |
19 | ) | |
20 | ); | |
21 | ||
22 | add_action('admin_menu', 'nick_add_authorofsermon_1_meta_box'); | |
23 | function nick_add_authorofsermon_1_meta_box() { | |
24 | ||
25 | global $authorofsermon_1_metabox; | |
26 | ||
27 | foreach($authorofsermon_1_metabox['page'] as $page) { | |
28 | add_meta_box($authorofsermon_1_metabox['id'], $authorofsermon_1_metabox['title'], 'nick_show_authorofsermon_1_box', $page, 'normal', 'high', $authorofsermon_1_metabox); | |
29 | } | |
30 | } | |
31 | ||
32 | // function to show meta boxes | |
33 | function nick_show_authorofsermon_1_box() { | |
34 | global $post; | |
35 | global $authorofsermon_1_metabox; | |
36 | global $nick_prefix; | |
37 | global $wp_version; | |
38 | ||
39 | // Use nonce for verification | |
40 | echo '<input type="hidden" name="nick_authorofsermon_1_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; | |
41 | ||
42 | echo '<table class="form-table">'; | |
43 | ||
44 | foreach ($authorofsermon_1_metabox['fields'] as $field) { | |
45 | // get current post meta data | |
46 | ||
47 | $meta = get_post_meta($post->ID, $field['id'], true); | |
48 | ||
49 | echo '<tr>', | |
50 | '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>', | |
51 | '<td class="nick_field_type_' . str_replace(' ', '_', $field['type']) . '">'; | |
52 | switch ($field['type']) { | |
53 | case 'text': | |
54 | echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /><br/>', '', stripslashes($field['desc']); | |
55 | break; | |
56 | case 'date': | |
57 | if($meta) { $value = nick_timestamp_to_date($meta); } else { $value = ''; } | |
58 | echo '<input type="text" class="nick_datepicker" name="' . $field['id'] . '" id="' . $field['id'] . '" value="'. $value . '" size="30" style="width:97%" />' . '' . stripslashes($field['desc']); | |
59 | break; | |
60 | case 'upload': | |
61 | echo '<input type="text" class="nick_upload_field" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:80%" /><input class="nick_upload_image_button" type="button" value="Upload Image" /><br/>', '', stripslashes($field['desc']); | |
62 | break; | |
63 | case 'textarea': | |
64 | ||
65 | if($field['rich_editor'] == 1) { | |
66 | if($wp_version >= 3.3) { | |
67 | echo wp_editor($meta, $field['id'], array('textarea_name' => $field['id'])); | |
68 | } else { | |
69 | // older versions of WP | |
70 | $editor = ''; | |
71 | if(!post_type_supports($post->post_type, 'editor')) { | |
72 | $editor = wp_tiny_mce(true, array('editor_selector' => $field['class'], 'remove_linebreaks' => false) ); | |
73 | } | |
74 | $field_html = '<div style="width: 97%; border: 1px solid #DFDFDF;"><textarea name="' . $field['id'] . '" class="' . $field['class'] . '" id="' . $field['id'] . '" cols="60" rows="8" style="width:100%">'. $meta . '</textarea></div><br/>' . __(stripslashes($field['desc'])); | |
75 | echo $editor . $field_html; | |
76 | } | |
77 | } else { | |
78 | echo '<div style="width: 100%;"><textarea name="', $field['id'], '" class="', $field['class'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea></div>', '', stripslashes($field['desc']); | |
79 | } | |
80 | ||
81 | break; | |
82 | case 'select': | |
83 | echo '<select name="', $field['id'], '" id="', $field['id'], '">'; | |
84 | foreach ($field['options'] as $option) { | |
85 | echo '<option value="' . $option . '"', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>'; | |
86 | } | |
87 | echo '</select>', '', stripslashes($field['desc']); | |
88 | break; | |
89 | case 'radio': | |
90 | foreach ($field['options'] as $option) { | |
91 | echo '<input type="radio" name="', $field['id'], '" value="', $option, '"', $meta == $option ? ' checked="checked"' : '', ' /> ', $option; | |
92 | } | |
93 | echo '<br/>' . stripslashes($field['desc']); | |
94 | break; | |
95 | case 'multicheck': | |
96 | foreach ($field['options'] as $option) { | |
97 | echo '<input type="checkbox" name="' . $field['id'] . '[' . $option . ']" value="' . $option . '"' . checked( true, in_array( $option, $meta ), false ) . '/> ' . $option; | |
98 | } | |
99 | echo '<br/>' . stripslashes($field['desc']); | |
100 | break; | |
101 | case 'checkbox': | |
102 | echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /> '; | |
103 | echo stripslashes($field['desc']); | |
104 | break; | |
105 | case 'slider': | |
106 | echo '<input type="text" rel="' . $field['max'] . '" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $meta . '" size="1" style="float: left; margin-right: 5px" />'; | |
107 | echo '<div class="nick-slider" rel="' . $field['id'] . '" style="float: left; width: 60%; margin: 5px 0 0 0;"></div>'; | |
108 | echo '<div style="width: 100%; clear: both;">' . stripslashes($field['desc']) . '</div>'; | |
109 | break; | |
110 | case 'repeatable' : | |
111 | ||
112 | $field_html = '<input type="hidden" id="' . $field['id'] . '" class="nick_repeatable_field_name" value=""/>'; | |
113 | if(is_array($meta)) { | |
114 | $count = 1; | |
115 | foreach($meta as $key => $value) { | |
116 | $field_html .= '<div class="nick_repeatable_wrapper"><input type="text" class="nick_repeatable_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta[$key] . '" size="30" style="width:90%" />'; | |
117 | if($count > 1) { | |
118 | $field_html .= '<a href="#" class="nick_remove_repeatable button-secondary">x</a><br/>'; | |
119 | } | |
120 | $field_html .= '</div>'; | |
121 | $count++; | |
122 | } | |
123 | } else { | |
124 | $field_html .= '<div class="nick_repeatable_wrapper"><input type="text" class="nick_repeatable_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta . '" size="30" style="width:90%" /></div>'; | |
125 | } | |
126 | $field_html .= '<button class="nick_add_new_field button-secondary">' . __('Add New', 'nick') . '</button> ' . __(stripslashes($field['desc'])); | |
127 | ||
128 | echo $field_html; | |
129 | ||
130 | break; | |
131 | ||
132 | case 'repeatable upload' : | |
133 | ||
134 | $field_html = '<input type="hidden" id="' . $field['id'] . '" class="nick_repeatable_upload_field_name" value=""/>'; | |
135 | if(is_array($meta)) { | |
136 | $count = 1; | |
137 | foreach($meta as $key => $value) { | |
138 | $field_html .= '<div class="nick_repeatable_upload_wrapper"><input type="text" class="nick_repeatable_upload_field nick_upload_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta[$key] . '" size="30" style="width:80%" /><button class="button-secondary nick_upload_image_button">Upload File</button>'; | |
139 | if($count > 1) { | |
140 | $field_html .= '<a href="#" class="nick_remove_repeatable button-secondary">x</a><br/>'; | |
141 | } | |
142 | $field_html .= '</div>'; | |
143 | $count++; | |
144 | } | |
145 | } else { | |
146 | $field_html .= '<div class="nick_repeatable_upload_wrapper"><input type="text" class="nick_repeatable_upload_field nick_upload_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta . '" size="30" style="width:80%" /><input class="button-secondary nick_upload_image_button" type="button" value="Upload File" /></div>'; | |
147 | } | |
148 | $field_html .= '<button class="nick_add_new_upload_field button-secondary">' . __('Add New', 'nick') . '</button> ' . __(stripslashes($field['desc'])); | |
149 | ||
150 | echo $field_html; | |
151 | ||
152 | break; | |
153 | } | |
154 | echo '<td>', | |
155 | '</tr>'; | |
156 | } | |
157 | ||
158 | echo '</table>'; | |
159 | } | |
160 | ||
161 | // Save data from meta box | |
162 | add_action('save_post', 'nick_authorofsermon_1_save'); | |
163 | function nick_authorofsermon_1_save($post_id) { | |
164 | global $post; | |
165 | global $authorofsermon_1_metabox; | |
166 | ||
167 | // verify nonce | |
168 | if (!wp_verify_nonce($_POST['nick_authorofsermon_1_meta_box_nonce'], basename(__FILE__))) { | |
169 | return $post_id; | |
170 | } | |
171 | ||
172 | // check autosave | |
173 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
174 | return $post_id; | |
175 | } | |
176 | ||
177 | // check permissions | |
178 | if ('page' == $_POST['post_type']) { | |
179 | if (!current_user_can('edit_page', $post_id)) { | |
180 | return $post_id; | |
181 | } | |
182 | } elseif (!current_user_can('edit_post', $post_id)) { | |
183 | return $post_id; | |
184 | } | |
185 | ||
186 | foreach ($authorofsermon_1_metabox['fields'] as $field) { | |
187 | ||
188 | $old = get_post_meta($post_id, $field['id'], true); | |
189 | $new = $_POST[$field['id']]; | |
190 | ||
191 | if ($new && $new != $old) { | |
192 | if($field['type'] == 'date') { | |
193 | $new = nick_format_date($new); | |
194 | update_post_meta($post_id, $field['id'], $new); | |
195 | } else { | |
196 | if(is_string($new)) { | |
197 | $new = $new; | |
198 | } | |
199 | update_post_meta($post_id, $field['id'], $new); | |
200 | ||
201 | ||
202 | } | |
203 | } elseif ('' == $new && $old) { | |
204 | delete_post_meta($post_id, $field['id'], $old); | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | ||
210 | function nick_export_ui_scripts() { | |
211 | ||
212 | global $nick_options, $post; | |
213 | ?> | |
214 | <script type="text/javascript"> | |
215 | jQuery(document).ready(function($) | |
216 | { | |
217 | ||
218 | if($('.form-table .nick_upload_field').length > 0 ) { | |
219 | // Media Uploader | |
220 | window.formfield = ''; | |
221 | ||
222 | $('.nick_upload_image_button').live('click', function() { | |
223 | window.formfield = $('.nick_upload_field',$(this).parent()); | |
224 | tb_show('', 'media-upload.php?type=file&post_id=<?php echo $post->ID; ?>&TB_iframe=true'); | |
225 | return false; | |
226 | }); | |
227 | ||
228 | window.original_send_to_editor = window.send_to_editor; | |
229 | window.send_to_editor = function(html) { | |
230 | if (window.formfield) { | |
231 | imgurl = $('a','<div>'+html+'</div>').attr('href'); | |
232 | window.formfield.val(imgurl); | |
233 | tb_remove(); | |
234 | } | |
235 | else { | |
236 | window.original_send_to_editor(html); | |
237 | } | |
238 | window.formfield = ''; | |
239 | window.imagefield = false; | |
240 | } | |
241 | } | |
242 | if($('.form-table .nick-slider').length > 0 ) { | |
243 | $('.nick-slider').each(function(){ | |
244 | var $this = $(this); | |
245 | var id = $this.attr('rel'); | |
246 | var val = $('#' + id).val(); | |
247 | var max = $('#' + id).attr('rel'); | |
248 | max = parseInt(max); | |
249 | //var step = $('#' + id).closest('input').attr('rel'); | |
250 | $this.slider({ | |
251 | value: val, | |
252 | max: max, | |
253 | step: 1, | |
254 | slide: function(event, ui) { | |
255 | $('#' + id).val(ui.value); | |
256 | } | |
257 | }); | |
258 | }); | |
259 | } | |
260 | ||
261 | if($('.form-table .nick_datepicker').length > 0 ) { | |
262 | var dateFormat = 'mm/dd/yy'; | |
263 | $('.nick_datepicker').datepicker({dateFormat: dateFormat}); | |
264 | } | |
265 | ||
266 | // add new repeatable field | |
267 | $(".nick_add_new_field").on('click', function() { | |
268 | var field = $(this).closest('td').find("div.nick_repeatable_wrapper:last").clone(true); | |
269 | var fieldLocation = $(this).closest('td').find('div.nick_repeatable_wrapper:last'); | |
270 | // set the new field val to blank | |
271 | $('input', field).val(""); | |
272 | field.insertAfter(fieldLocation, $(this).closest('td')); | |
273 | ||
274 | return false; | |
275 | }); | |
276 | ||
277 | // add new repeatable upload field | |
278 | $(".nick_add_new_upload_field").on('click', function() { | |
279 | var container = $(this).closest('tr'); | |
280 | var field = $(this).closest('td').find("div.nick_repeatable_upload_wrapper:last").clone(true); | |
281 | var fieldLocation = $(this).closest('td').find('div.nick_repeatable_upload_wrapper:last'); | |
282 | // set the new field val to blank | |
283 | $('input[type="text"]', field).val(""); | |
284 | ||
285 | field.insertAfter(fieldLocation, $(this).closest('td')); | |
286 | ||
287 | return false; | |
288 | }); | |
289 | ||
290 | // remove repeatable field | |
291 | $('.nick_remove_repeatable').on('click', function(e) { | |
292 | e.preventDefault(); | |
293 | var field = $(this).parent(); | |
294 | $('input', field).val(""); | |
295 | field.remove(); | |
296 | return false; | |
297 | }); | |
298 | ||
299 | }); | |
300 | </script> | |
301 | <?php | |
302 | } | |
303 | ||
304 | function nick_export_datepicker_ui_scripts() { | |
305 | global $nick_base_dir; | |
306 | wp_enqueue_script('jquery-ui-datepicker'); | |
307 | wp_enqueue_script('jquery-ui-slider'); | |
308 | } | |
309 | function nick_export_datepicker_ui_styles() { | |
310 | global $nick_base_dir; | |
311 | wp_enqueue_style('jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css', false, '1.8', 'all'); | |
312 | } | |
313 | ||
314 | // these are for newest versions of WP | |
315 | add_action('admin_print_scripts-post.php', 'nick_export_datepicker_ui_scripts'); | |
316 | add_action('admin_print_scripts-edit.php', 'nick_export_datepicker_ui_scripts'); | |
317 | add_action('admin_print_scripts-post-new.php', 'nick_export_datepicker_ui_scripts'); | |
318 | add_action('admin_print_styles-post.php', 'nick_export_datepicker_ui_styles'); | |
319 | add_action('admin_print_styles-edit.php', 'nick_export_datepicker_ui_styles'); | |
320 | add_action('admin_print_styles-post-new.php', 'nick_export_datepicker_ui_styles'); | |
321 | ||
322 | if ((isset($_GET['post']) && (isset($_GET['action']) && $_GET['action'] == 'edit') ) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php'))) | |
323 | { | |
324 | add_action('admin_head', 'nick_export_ui_scripts'); | |
325 | } | |
326 | ||
327 | // converts a time stamp to date string for meta fields | |
328 | if(!function_exists('nick_timestamp_to_date')) { | |
329 | function nick_timestamp_to_date($date) { | |
330 | ||
331 | return date('m/d/Y', $date); | |
332 | } | |
333 | } | |
334 | if(!function_exists('nick_format_date')) { | |
335 | function nick_format_date($date) { | |
336 | ||
337 | $date = strtotime($date); | |
338 | ||
339 | return $date; | |
340 | } | |
341 | } |