Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 571 lines (506 sloc) 24.809 kb
babc508 @norcross taxonomy multi-check cleanup
norcross authored
1 <?php
2 /*
3 Script Name: Custom Metaboxes and Fields
4 Contributors: Andrew Norcross (@norcross / andrewnorcross.com)
5 Jared Atchison (@jaredatch / jaredatchison.com)
6 Bill Erickson (@billerickson / billerickson.net)
7 Description: This will create metaboxes with custom fields that will blow your mind.
8 Version: 0.9
9 */
10
11 /**
12 * Released under the GPL license
13 * http://www.opensource.org/licenses/gpl-license.php
14 *
15 * This is an add-on for WordPress
16 * http://wordpress.org/
17 *
18 * **********************************************************************
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 * **********************************************************************
29 */
30
31 /************************************************************************
32 You should not edit the code below or things might explode!
33 *************************************************************************/
34
35 $meta_boxes = array();
36 $meta_boxes = apply_filters ( 'cmb_meta_boxes' , $meta_boxes );
37 foreach ( $meta_boxes as $meta_box ) {
38 $my_box = new cmb_Meta_Box( $meta_box );
39 }
40
41 /**
42 * Validate value of meta fields
43 * Define ALL validation methods inside this class and use the names of these
44 * methods in the definition of meta boxes (key 'validate_func' of each field)
45 */
46 class cmb_Meta_Box_Validate {
47 function check_text( $text ) {
48 if ($text != 'hello') {
49 return false;
50 }
51 return true;
52 }
53 }
54
55 /**
56 * Defines the url to which is used to load local resources.
57 * This may need to be filtered for local Window installations.
58 * If resources do not load, please check the wiki for details.
59 */
60 define( 'CMB_META_BOX_URL', apply_filters( 'cmb_meta_box_url', trailingslashit( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, dirname( __FILE__ ) ) ) ) );
61
62 /**
63 * Create meta boxes
64 */
65 class cmb_Meta_Box {
66 protected $_meta_box;
67
68 function __construct( $meta_box ) {
69 if ( !is_admin() ) return;
70
71 $this->_meta_box = $meta_box;
72
73 $upload = false;
74 foreach ( $meta_box['fields'] as $field ) {
75 if ( $field['type'] == 'file' || $field['type'] == 'file_list' ) {
76 $upload = true;
77 break;
78 }
79 }
80
81 global $pagenow;
82 if ( $upload && in_array( $pagenow, array( 'page.php', 'page-new.php', 'post.php', 'post-new.php' ) ) ) {
83 add_action( 'admin_head', array( &$this, 'add_post_enctype' ) );
84 }
85
86 add_action( 'admin_menu', array( &$this, 'add' ) );
87 add_action( 'save_post', array( &$this, 'save' ) );
88
89 add_filter( 'cmb_show_on', array( &$this, 'add_for_id' ), 10, 2 );
90 add_filter( 'cmb_show_on', array( &$this, 'add_for_page_template' ), 10, 2 );
91 }
92
93 function add_post_enctype() {
94 echo '
95 <script type="text/javascript">
96 jQuery(document).ready(function(){
97 jQuery("#post").attr("enctype", "multipart/form-data");
98 jQuery("#post").attr("encoding", "multipart/form-data");
99 });
100 </script>';
101 }
102
103 // Add metaboxes
104 function add() {
105 $this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context'];
106 $this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'high' : $this->_meta_box['priority'];
107 $this->_meta_box['show_on'] = empty( $this->_meta_box['show_on'] ) ? array('key' => false, 'value' => false) : $this->_meta_box['show_on'];
108
109 foreach ( $this->_meta_box['pages'] as $page ) {
110 if( apply_filters( 'cmb_show_on', true, $this->_meta_box ) )
111 add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']) ;
112 }
113 }
114
115 /**
116 * Show On Filters
117 * Use the 'cmb_show_on' filter to further refine the conditions under which a metabox is displayed.
118 * Below you can limit it by ID and page template
119 */
120
121 // Add for ID
122 function add_for_id( $display, $meta_box ) {
123 if ( 'id' !== $meta_box['show_on']['key'] )
124 return $display;
125
126 // If we're showing it based on ID, get the current ID
127 if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
128 elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
129 if( !isset( $post_id ) )
130 return false;
131
132 // If value isn't an array, turn it into one
133 $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
134
135 // If current page id is in the included array, display the metabox
136
137 if ( in_array( $post_id, $meta_box['show_on']['value'] ) )
138 return true;
139 else
140 return false;
141 }
142
143 // Add for Page Template
144 function add_for_page_template( $display, $meta_box ) {
145 if( 'page-template' !== $meta_box['show_on']['key'] )
146 return $display;
147
148 // Get the current ID
149 if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
150 elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
151 if( !( isset( $post_id ) || is_page() ) ) return false;
152
153 // Get current template
154 $current_template = get_post_meta( $post_id, '_wp_page_template', true );
155
156 // If value isn't an array, turn it into one
157 $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
158
159 // See if there's a match
160 if( in_array( $current_template, $meta_box['show_on']['value'] ) )
161 return true;
162 else
163 return false;
164 }
165
166 // Show fields
167 function show() {
168
169 global $post;
170
171 // Use nonce for verification
172 echo '<input type="hidden" name="wp_meta_box_nonce" value="', wp_create_nonce( basename(__FILE__) ), '" />';
173 echo '<table class="form-table cmb_metabox">';
174
175 foreach ( $this->_meta_box['fields'] as $field ) {
176 // Set up blank or default values for empty ones
177 if ( !isset( $field['name'] ) ) $field['name'] = '';
178 if ( !isset( $field['desc'] ) ) $field['desc'] = '';
179 if ( !isset( $field['std'] ) ) $field['std'] = '';
180 if ( 'file' == $field['type'] && !isset( $field['allow'] ) ) $field['allow'] = array( 'url', 'attachment' );
181 if ( 'file' == $field['type'] && !isset( $field['save_id'] ) ) $field['save_id'] = false;
182 if ( 'multicheck' == $field['type'] ) $field['multiple'] = true;
183
184 $meta = get_post_meta( $post->ID, $field['id'], 'multicheck' != $field['type'] /* If multicheck this can be multiple values */ );
185
186 echo '<tr>';
187
188 if ( $field['type'] == "title" ) {
189 echo '<td colspan="2">';
190 } else {
191 if( $this->_meta_box['show_names'] == true ) {
192 echo '<th style="width:18%"><label for="', $field['id'], '">', $field['name'], '</label></th>';
193 }
194 echo '<td>';
195 }
196
197 switch ( $field['type'] ) {
198
199 case 'text':
200 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" />','<p class="cmb_metabox_description">', $field['desc'], '</p>';
201 break;
202 case 'text_small':
203 echo '<input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
204 break;
205 case 'text_medium':
206 echo '<input class="cmb_text_medium" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
207 break;
208 case 'text_date':
209 echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
210 break;
211 case 'text_date_timestamp':
212 echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? date( 'm\/d\/Y', $meta ) : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
213 break;
214
215 case 'text_datetime_timestamp':
216 echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '[date]" id="', $field['id'], '_date" value="', '' !== $meta ? date( 'm\/d\/Y', $meta ) : $field['std'], '" />';
217 echo '<input class="cmb_timepicker text_time" type="text" name="', $field['id'], '[time]" id="', $field['id'], '_time" value="', '' !== $meta ? date( 'h:i A', $meta ) : $field['std'], '" /><span class="cmb_metabox_description" >', $field['desc'], '</span>';
218 break;
219 case 'text_time':
220 echo '<input class="cmb_timepicker text_time" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
221 break;
222 case 'text_money':
223 echo '$ <input class="cmb_text_money" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
224 break;
225 case 'colorpicker':
226 $meta = '' !== $meta ? $meta : $field['std'];
227 $hex_color = '(([a-fA-F0-9]){3}){1,2}$';
228 if ( preg_match( '/^' . $hex_color . '/i', $meta ) ) // Value is just 123abc, so prepend #.
229 $meta = '#' . $meta;
230 elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta ) ) // Value doesn't match #123abc, so sanitize to just #.
231 $meta = "#";
232 echo '<input class="cmb_colorpicker cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta, '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
233 break;
234 case 'textarea':
235 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="10">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
236 break;
237 case 'textarea_small':
238 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
239 break;
240 case 'textarea_code':
241 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="10" class="cmb_textarea_code">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
242 break;
243 case 'select':
244 if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
245 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
246 foreach ($field['options'] as $option) {
247 echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
248 }
249 echo '</select>';
250 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
251 break;
252 case 'radio_inline':
253 if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
254 echo '<div class="cmb_radio_inline">';
255 $i = 1;
256 foreach ($field['options'] as $option) {
257 echo '<div class="cmb_radio_inline_option"><input type="radio" name="', $field['id'], '" id="', $field['id'], $i, '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $option['name'], '</label></div>';
258 $i++;
259 }
260 echo '</div>';
261 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
262 break;
263 case 'radio':
264 if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
265 echo '<ul>';
266 $i = 1;
267 foreach ($field['options'] as $option) {
268 echo '<li><input type="radio" name="', $field['id'], '" id="', $field['id'], $i,'" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $option['name'].'</label></li>';
269 $i++;
270 }
271 echo '</ul>';
272 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
273 break;
274 case 'checkbox':
275 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
276 echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
277 break;
278 case 'multicheck':
279 echo '<ul>';
280 $i = 1;
281 foreach ( $field['options'] as $value => $name ) {
282 // Append `[]` to the name to get multiple values
283 // Use in_array() to check whether the current option should be checked
284 echo '<li><input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], $i, '" value="', $value, '"', in_array( $value, $meta ) ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $name, '</label></li>';
285 $i++;
286 }
287 echo '</ul>';
288 echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
289 break;
290 case 'title':
291 echo '<h5 class="cmb_metabox_title">', $field['name'], '</h5>';
292 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
293 break;
294 case 'wysiwyg':
295 wp_editor( $meta ? $meta : $field['std'], $field['id'], isset( $field['options'] ) ? $field['options'] : array() );
296 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
297 break;
298 case 'taxonomy_select':
299 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
300 $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
301 $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
302 foreach ( $terms as $term ) {
303 if (!is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
304 echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>';
305 } else {
306 echo '<option value="' . $term->slug . ' ' , $meta == $term->slug ? $meta : ' ' ,' ">' . $term->name . '</option>';
307 }
308 }
309 echo '</select>';
310 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
311 break;
312 case 'taxonomy_radio':
313 $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
314 $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
315 echo '<ul>';
316 foreach ( $terms as $term ) {
317 if ( !is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
318 echo '<li><input type="radio" name="', $field['id'], '" value="'. $term->slug . '" checked>' . $term->name . '</li>';
319 } else {
320 echo '<li><input type="radio" name="', $field['id'], '" value="' . $term->slug . ' ' , $meta == $term->slug ? $meta : ' ' ,' ">' . $term->name .'</li>';
321 }
322 }
323 echo '</ul>';
324 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
325 break;
326 case 'taxonomy_multicheck':
327 echo '<ul>';
328 $names = wp_get_object_terms( $post->ID, $field['taxonomy'] );
329 $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
330 foreach ($terms as $term) {
331 echo '<li><input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], '" value="', $term->name , '"';
332 foreach ($names as $name) {
333 if ( $term->slug == $name->slug ){ echo ' checked="checked" ';};
334 }
335 echo' /><label>', $term->name , '</label></li>';
336 }
337 echo '</ul>';
338 echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
339 break;
340 case 'file_list':
341 echo '<input class="cmb_upload_file" type="text" size="36" name="', $field['id'], '" value="" />';
342 echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
343 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
344 $args = array(
345 'post_type' => 'attachment',
346 'numberposts' => null,
347 'post_status' => null,
348 'post_parent' => $post->ID
349 );
350 $attachments = get_posts($args);
351 if ($attachments) {
352 echo '<ul class="attach_list">';
353 foreach ($attachments as $attachment) {
354 echo '<li>'.wp_get_attachment_link($attachment->ID, 'thumbnail', 0, 0, 'Download');
355 echo '<span>';
356 echo apply_filters('the_title', '&nbsp;'.$attachment->post_title);
357 echo '</span></li>';
358 }
359 echo '</ul>';
360 }
361 break;
362 case 'file':
363 $input_type_url = "hidden";
364 if ( 'url' == $field['allow'] || ( is_array( $field['allow'] ) && in_array( 'url', $field['allow'] ) ) )
365 $input_type_url="text";
366 echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['id'], '" name="', $field['id'], '" value="', $meta, '" />';
367 echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
368 echo '<input class="cmb_upload_file_id" type="hidden" id="', $field['id'], '_id" name="', $field['id'], '_id" value="', get_post_meta( $post->ID, $field['id'] . "_id",true), '" />';
369 echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
370 echo '<div id="', $field['id'], '_status" class="cmb_upload_status">';
371 if ( $meta != '' ) {
372 $check_image = preg_match( '/(^.*\.jpg|jpeg|png|gif|ico*)/i', $meta );
373 if ( $check_image ) {
374 echo '<div class="img_status">';
375 echo '<img src="', $meta, '" alt="" />';
376 echo '<a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove Image</a>';
377 echo '</div>';
378 } else {
379 $parts = explode( '/', $meta );
380 for( $i = 0; $i < count( $parts ); ++$i ) {
381 $title = $parts[$i];
382 }
383 echo 'File: <strong>', $title, '</strong>&nbsp;&nbsp;&nbsp; (<a href="', $meta, '" target="_blank" rel="external">Download</a> / <a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove</a>)';
384 }
385 }
386 echo '</div>';
387 break;
388 default:
389 do_action('cmb_render_' . $field['type'] , $field, $meta);
390 }
391
392 echo '</td>','</tr>';
393 }
394 echo '</table>';
395 }
396
397 // Save data from metabox
398 function save( $post_id) {
399
400 // verify nonce
401 if ( ! isset( $_POST['wp_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename(__FILE__) ) ) {
402 return $post_id;
403 }
404
405 // check autosave
406 if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
407 return $post_id;
408 }
409
410 // check permissions
411 if ( 'page' == $_POST['post_type'] ) {
412 if ( !current_user_can( 'edit_page', $post_id ) ) {
413 return $post_id;
414 }
415 } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
416 return $post_id;
417 }
418
419 foreach ( $this->_meta_box['fields'] as $field ) {
420 $name = $field['id'];
421
422 if ( ! isset( $field['multiple'] ) )
423 $field['multiple'] = ( 'multicheck' == $field['type'] ) ? true : false;
424
425 $old = get_post_meta( $post_id, $name, !$field['multiple'] /* If multicheck this can be multiple values */ );
426 $new = isset( $_POST[$field['id']] ) ? $_POST[$field['id']] : null;
427
428 if ( in_array( $field['type'], array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_multicheck' ) ) ) {
429 $new = wp_set_object_terms( $post_id, $new, $field['taxonomy'] );
430 }
431
432 if ( ($field['type'] == 'textarea') || ($field['type'] == 'textarea_small') ) {
433 $new = htmlspecialchars( $new );
434 }
435
436 if ( ($field['type'] == 'textarea_code') ) {
437 $new = htmlspecialchars_decode( $new );
438 }
439
440 if ( $field['type'] == 'text_date_timestamp' ) {
441 $new = strtotime( $new );
442 }
443
444 if ( $field['type'] == 'text_datetime_timestamp' ) {
445 $string = $new['date'] . ' ' . $new['time'];
446 $new = strtotime( $string );
447 }
448
449 $new = apply_filters('cmb_validate_' . $field['type'], $new, $post_id, $field);
450
451 // validate meta value
452 if ( isset( $field['validate_func']) ) {
453 $ok = call_user_func( array( 'cmb_Meta_Box_Validate', $field['validate_func']), $new );
454 if ( $ok === false ) { // pass away when meta value is invalid
455 continue;
456 }
457 } elseif ( $field['multiple'] ) {
458 delete_post_meta( $post_id, $name );
459 if ( !empty( $new ) ) {
460 foreach ( $new as $add_new ) {
461 add_post_meta( $post_id, $name, $add_new, false );
462 }
463 }
464 } elseif ( '' !== $new && $new != $old ) {
465 update_post_meta( $post_id, $name, $new );
466 } elseif ( '' == $new ) {
467 delete_post_meta( $post_id, $name );
468 }
469
470 if ( 'file' == $field['type'] ) {
471 $name = $field['id'] . "_id";
472 $old = get_post_meta( $post_id, $name, !$field['multiple'] /* If multicheck this can be multiple values */ );
473 if ( isset( $field['save_id'] ) && $field['save_id'] ) {
474 $new = isset( $_POST[$name] ) ? $_POST[$name] : null;
475 } else {
476 $new = "";
477 }
478
479 if ( $new && $new != $old ) {
480 update_post_meta( $post_id, $name, $new );
481 } elseif ( '' == $new && $old ) {
482 delete_post_meta( $post_id, $name, $old );
483 }
484 }
485 }
486 }
487 }
488
489 /**
490 * Adding scripts and styles
491 */
492 function cmb_scripts( $hook ) {
493 if ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'page-new.php' || $hook == 'page.php' ) {
494 wp_register_script( 'cmb-timepicker', CMB_META_BOX_URL . 'js/jquery.timePicker.min.js' );
495 wp_register_script( 'cmb-scripts', CMB_META_BOX_URL . 'js/cmb.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox', 'farbtastic' ) );
496 wp_enqueue_script( 'cmb-timepicker' );
497 wp_enqueue_script( 'cmb-scripts' );
498 wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style.css', array( 'thickbox', 'farbtastic' ) );
499 wp_enqueue_style( 'cmb-styles' );
500 }
501 }
502 add_action( 'admin_enqueue_scripts', 'cmb_scripts', 10 );
503
504 function cmb_editor_footer_scripts() { ?>
505 <?php
506 if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] ) {
507 $label = $_GET['cmb_send_label'];
508 if ( empty( $label ) ) $label="Select File";
509 ?>
510 <script type="text/javascript">
511 jQuery(function($) {
512 $('td.savesend input').val('<?php echo $label; ?>');
513 });
514 </script>
515 <?php
516 }
517 }
518 add_action( 'admin_print_footer_scripts', 'cmb_editor_footer_scripts', 99 );
519
520 // Force 'Insert into Post' button from Media Library
521 add_filter( 'get_media_item_args', 'cmb_force_send' );
522 function cmb_force_send( $args ) {
523
524 // if the Gallery tab is opened from a custom meta box field, add Insert Into Post button
525 if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] )
526 $args['send'] = true;
527
528 // if the From Computer tab is opened AT ALL, add Insert Into Post button after an image is uploaded
529 if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
530
531 $args['send'] = true;
532
533 // TO DO: Are there any conditions in which we don't want the Insert Into Post
534 // button added? For example, if a post type supports thumbnails, does not support
535 // the editor, and does not have any cmb file inputs? If so, here's the first
536 // bits of code needed to check all that.
537 // $attachment_ancestors = get_post_ancestors( $_POST["attachment_id"] );
538 // $attachment_parent_post_type = get_post_type( $attachment_ancestors[0] );
539 // $post_type_object = get_post_type_object( $attachment_parent_post_type );
540 }
541
542 // change the label of the button on the From Computer tab
543 if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
544
545 echo '
546 <script type="text/javascript">
547 function cmbGetParameterByNameInline(name) {
548 name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
549 var regexS = "[\\?&]" + name + "=([^&#]*)";
550 var regex = new RegExp(regexS);
551 var results = regex.exec(window.location.href);
552 if(results == null)
553 return "";
554 else
555 return decodeURIComponent(results[1].replace(/\+/g, " "));
556 }
557
558 jQuery(function($) {
559 if (cmbGetParameterByNameInline("cmb_force_send")=="true") {
560 var cmb_send_label = cmbGetParameterByNameInline("cmb_send_label");
561 $("td.savesend input").val(cmb_send_label);
562 }
563 });
564 </script>
565 ';
566 }
567
568 return $args;
569
570 }
8ca945c @billerickson limit metabox to page template
billerickson authored
571 // End. That's it, folks! //
Something went wrong with that request. Please try again.