• 1.
     
  • 2.
    /**
  • 3.
     * Add a custom Meta Box
  • 4.
     *
  • 5.
     * @param array $meta_box Meta box input data
  • 6.
     */
  • 7.
    function icy_add_meta_box( $meta_box )
  • 8.
    {
  • 9.
        if( !is_array($meta_box) ) return false;
  • 10.
        
  • 11.
        // Create a callback function
  • 12.
        $callback = create_function( '$post,$meta_box', 'icy_create_meta_box( $post, $meta_box["args"] );' );
  • 13.
     
  • 14.
        add_meta_box( $meta_box['id'], $meta_box['title'], $callback, $meta_box['page'], $meta_box['context'], $meta_box['priority'], $meta_box );
  • 15.
    }
  • 16.
     
  • 17.
    /**
  • 18.
     * Create content for a custom Meta Box
  • 19.
     *
  • 20.
     * @param array $meta_box Meta box input data
  • 21.
     */
  • 22.
    function icy_create_meta_box( $post, $meta_box )
  • 23.
    {
  • 24.
    	// set up for fallback to old way of doing things
  • 25.
    	$wp_version = get_bloginfo('version');
  • 26.
    	
  • 27.
        if( !is_array($meta_box) ) return false;
  • 28.
        
  • 29.
        if( isset($meta_box['description']) && $meta_box['description'] != '' ){
  • 30.
        	echo '<p>'. $meta_box['description'] .'</p>';
  • 31.
        }
  • 32.
        
  • 33.
    	wp_nonce_field( basename(__FILE__), 'icy_meta_box_nonce' );
  • 34.
    	echo '<table class="form-table icy-metabox-table">';
  • 35.
     
  • 36.
    	foreach( $meta_box['fields'] as $field ){
  • 37.
    		// Get current post meta data
  • 38.
    		$meta = get_post_meta( $post->ID, $field['id'], true );
  • 39.
    		echo '<tr><th><label for="'. $field['id'] .'"><strong>'. $field['name'] .'</strong>
  • 40.
    			  <span>'. $field['desc'] .'</span></label></th>';
  • 41.
    		
  • 42.
    		switch( $field['type'] ){	
  • 43.
    			case 'text':
  • 44.
    				echo '<td><input type="text" name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['std']) .'" size="30" /></td>';
  • 45.
    				break;	
  • 46.
    				
  • 47.
    			case 'textarea':
  • 48.
    				echo '<td><textarea name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" rows="8" cols="5">'. ($meta ? $meta : $field['std']) .'</textarea></td>';
  • 49.
    				break;
  • 50.
    				
  • 51.
    			case 'file':
  • 52.
    				if( version_compare($wp_version, '3.4.2', '>') ) {
  • 53.
    			?> 
  • 54.
    				<script>
  • 55.
    				jQuery(function($) {
  • 56.
    					var frame;
  • 57.
     
  • 58.
    					$('#<?php echo $field['id']; ?>_button').on('click', function(e) {
  • 59.
    						e.preventDefault();
  • 60.
     
  • 61.
    						// Set options for 1st frame render
  • 62.
    						var options = {
  • 63.
    							state: 'insert',
  • 64.
    							frame: 'post'
  • 65.
    						};
  • 66.
     
  • 67.
    						frame = wp.media(options).open();
  • 68.
    						
  • 69.
    						// Tweak views
  • 70.
    						frame.menu.get('view').unset('gallery');
  • 71.
    						frame.menu.get('view').unset('featured-image');
  • 72.
    												
  • 73.
    						frame.toolbar.get('view').set({
  • 74.
    							insert: {
  • 75.
    								style: 'primary',
  • 76.
    								text: '<?php _e("Insert", "framework"); ?>',
  • 77.
     
  • 78.
    								click: function() {
  • 79.
    									var models = frame.state().get('selection'),
  • 80.
    										url = models.first().attributes.url;
  • 81.
     
  • 82.
    									$('#<?php echo $field['id']; ?>').val( url ); 
  • 83.
     
  • 84.
    									frame.close();
  • 85.
    								}
  • 86.
    							}
  • 87.
    						});
  • 88.
    						
  • 89.
     
  • 90.
    					});
  • 91.
    					
  • 92.
    				});
  • 93.
    				</script>
  • 94.
    			<?php
  • 95.
    				} // if version compare
  • 96.
    				echo '<td><input type="text" name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['std']) .'" size="30" class="file" /> <input type="button" class="button" name="'. $field['id'] .'_button" id="'. $field['id'] .'_button" value="Browse" /></td>';
  • 97.
    				break;
  • 98.
     
  • 99.
    			case 'images': 
  • 100.
    				if( version_compare($wp_version, '3.4.2', '>') ) {
  • 101.
    					// Using Wp3.5+
  • 102.
    			?>
  • 103.
    				<script>
  • 104.
    				jQuery(function($) {
  • 105.
    					var frame,
  • 106.
    					    images = '<?php echo get_post_meta( $post->ID, '_icy_image_ids', true ); ?>',
  • 107.
    					    selection = loadImages(images);
  • 108.
     
  • 109.
    					$('#icy_images_upload').on('click', function(e) {
  • 110.
    						e.preventDefault();
  • 111.
     
  • 112.
    						// Set options for 1st frame render
  • 113.
    						var options = {
  • 114.
    							title: '<?php _e("Create Featured Gallery", "framework"); ?>',
  • 115.
    							state: 'gallery-edit',
  • 116.
    							frame: 'post',
  • 117.
    							selection: selection
  • 118.
    						};
  • 119.
     
  • 120.
    						// Check if frame or gallery already exist
  • 121.
    						if( frame || selection ) {
  • 122.
    							options['title'] = '<?php _e("Edit Featured Gallery", "framework"); ?>';
  • 123.
    						}
  • 124.
     
  • 125.
    						frame = wp.media(options).open();
  • 126.
    						
  • 127.
    						// Tweak views
  • 128.
    						frame.menu.get('view').unset('cancel');
  • 129.
    						frame.menu.get('view').unset('separateCancel');
  • 130.
    						frame.menu.get('view').get('gallery-edit').el.innerHTML = '<?php _e("Edit Featured Gallery", "framework"); ?>';
  • 131.
    						frame.content.get('view').sidebar.unset('gallery'); // Hide Gallery Settings in sidebar
  • 132.
     
  • 133.
    						// When we are editing a gallery
  • 134.
    						overrideGalleryInsert();
  • 135.
    						frame.on( 'toolbar:render:gallery-edit', function() {
  • 136.
        						overrideGalleryInsert();
  • 137.
    						});
  • 138.
    						
  • 139.
    						frame.on( 'content:render:browse', function( browser ) {
  • 140.
    						    if ( !browser ) return;
  • 141.
    						    // Hide Gallery Settings in sidebar
  • 142.
    						    browser.sidebar.on('ready', function(){
  • 143.
    						        browser.sidebar.unset('gallery');
  • 144.
    						    });
  • 145.
    						    // Hide filter/search as they don't work
  • 146.
    						    browser.toolbar.on('ready', function(){
  • 147.
        						    if(browser.toolbar.controller._state == 'gallery-library'){
  • 148.
        						        browser.toolbar.$el.hide();
  • 149.
        						    }
  • 150.
    						    });
  • 151.
    						});
  • 152.
    						
  • 153.
    						// All images removed
  • 154.
    						frame.state().get('library').on( 'remove', function() {
  • 155.
    						    var models = frame.state().get('library');
  • 156.
    							if(models.length == 0){
  • 157.
    							    selection = false;
  • 158.
        							$.post(ajaxurl, { ids: '', action: 'icy_save_images', post_id: icy_ajax.post_id, nonce: icy_ajax.nonce });
  • 159.
    							}
  • 160.
    						});
  • 161.
    						
  • 162.
    						// Override insert button
  • 163.
    						function overrideGalleryInsert() {
  • 164.
        						frame.toolbar.get('view').set({
  • 165.
    								insert: {
  • 166.
    									style: 'primary',
  • 167.
    									text: '<?php _e("Save Featured Gallery", "framework"); ?>',
  • 168.
     
  • 169.
    									click: function() {
  • 170.
    										var models = frame.state().get('library'),
  • 171.
    										    ids = '';
  • 172.
     
  • 173.
    										models.each( function( attachment ) {
  • 174.
    										    ids += attachment.id + ','
  • 175.
    										});
  • 176.
     
  • 177.
    										this.el.innerHTML = '<?php _e("Saving...", "framework"); ?>';
  • 178.
    										
  • 179.
    										$.ajax({
  • 180.
    											type: 'POST',
  • 181.
    											url: ajaxurl,
  • 182.
    											data: { 
  • 183.
    												ids: ids, 
  • 184.
    												action: 'icy_save_images', 
  • 185.
    												post_id: icy_ajax.post_id, 
  • 186.
    												nonce: icy_ajax.nonce 
  • 187.
    											},
  • 188.
    											success: function(){
  • 189.
        											selection = loadImages(ids);
  • 190.
        											$('#_icy_image_ids').val( ids );
  • 191.
        											frame.close();
  • 192.
    											},
  • 193.
    											dataType: 'html'
  • 194.
    										}).done( function( data ) {
  • 195.
    											$('.icy-gallery-thumbs').html( data );
  • 196.
    										}); 
  • 197.
    									}
  • 198.
    								}
  • 199.
    							});
  • 200.
    						}
  • 201.
    					});
  • 202.
    					
  • 203.
    					// Load images
  • 204.
    					function loadImages(images) {
  • 205.
    						if( images ){
  • 206.
    						    var shortcode = new wp.shortcode({
  • 207.
                					tag:    'gallery',
  • 208.
                					attrs:   { ids: images },
  • 209.
                					type:   'single'
  • 210.
                				});
  • 211.
    				
  • 212.
    						    var attachments = wp.media.gallery.attachments( shortcode );
  • 213.
     
  • 214.
                				var selection = new wp.media.model.Selection( attachments.models, {
  • 215.
                					props:    attachments.props.toJSON(),
  • 216.
                					multiple: true
  • 217.
                				});
  • 218.
                
  • 219.
                				selection.gallery = attachments.gallery;
  • 220.
                
  • 221.
                				// Fetch the query's attachments, and then break ties from the
  • 222.
                				// query to allow for sorting.
  • 223.
                				selection.more().done( function() {
  • 224.
                					// Break ties with the query.
  • 225.
                					selection.props.set({ query: false });
  • 226.
                					selection.unmirror();
  • 227.
                					selection.props.unset('orderby');
  • 228.
                				});
  • 229.
                				
  • 230.
                				return selection;
  • 231.
    						}
  • 232.
    						
  • 233.
    						return false;
  • 234.
    					}
  • 235.
    					
  • 236.
    				});
  • 237.
    				</script>
  • 238.
    			<?php
  • 239.
    				// SPECIAL CASE:
  • 240.
    				// std controls button text; unique meta key for image uploads
  • 241.
    				$meta = get_post_meta( $post->ID, '_icy_image_ids', true );
  • 242.
    				$thumbs_output = '';
  • 243.
    				$button_text = ($meta) ? __('Edit Gallery', 'icy') : $field['std'];
  • 244.
    				if( $meta ) {
  • 245.
    					$field['std'] = __('Edit Gallery', 'icy');
  • 246.
    					$thumbs = explode(',', $meta);
  • 247.
    					$thumbs_output = '';
  • 248.
    					foreach( $thumbs as $thumb ) {
  • 249.
    						$thumbs_output .= '<li>' . wp_get_attachment_image( $thumb, array(32,32) ) . '</li>';
  • 250.
    					}
  • 251.
    				}
  • 252.
     
  • 253.
    			    echo 
  • 254.
    			    	'<td>
  • 255.
    			    		<input type="button" class="button" name="' . $field['id'] . '" id="icy_images_upload" value="' . $button_text .'" />
  • 256.
    			    		
  • 257.
    			    		<input type="hidden" name="icy_meta[_icy_image_ids]" id="_icy_image_ids" value="' . ($meta ? $meta : 'false') . '" />
  • 258.
     
  • 259.
    			    		<ul class="icy-gallery-thumbs">' . $thumbs_output . '</ul>
  • 260.
    			    	</td>';
  • 261.
    			    } else {
  • 262.
    			    	// Using pre-WP3.5
  • 263.
    			    	echo '<td><input type="button" class="button" name="' . $field['id'] . '" id="icy_images_upload" value="' . $field['std'] .'" /></td>';
  • 264.
    			    }
  • 265.
    			    break;
  • 266.
    				
  • 267.
    			case 'select':
  • 268.
    				echo'<td><select name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'">';
  • 269.
    				foreach( $field['options'] as $key => $option ){
  • 270.
    					echo '<option value="' . $key . '"';
  • 271.
    					if( $meta ){ 
  • 272.
    						if( $meta == $key ) echo ' selected="selected"'; 
  • 273.
    					} else {
  • 274.
    						if( $field['std'] == $key ) echo ' selected="selected"'; 
  • 275.
    					}
  • 276.
    					echo'>'. $option .'</option>';
  • 277.
    				}
  • 278.
    				echo'</select></td>';
  • 279.
    				break;
  • 280.
    				
  • 281.
    			case 'radio':
  • 282.
    				echo '<td>';
  • 283.
    				foreach( $field['options'] as $key => $option ){
  • 284.
    					echo '<label class="radio-label"><input type="radio" name="icy_meta['. $field['id'] .']" value="'. $key .'" class="radio"';
  • 285.
    					if( $meta ){ 
  • 286.
    						if( $meta == $key ) echo ' checked="checked"'; 
  • 287.
    					} else {
  • 288.
    						if( $field['std'] == $key ) echo ' checked="checked"';
  • 289.
    					}
  • 290.
    					echo ' /> '. $option .'</label> ';
  • 291.
    				}
  • 292.
    				echo '</td>';
  • 293.
    				break;
  • 294.
    			
  • 295.
    			case 'color':
  • 296.
    			    if( array_key_exists('val', $field) ) $val = ' value="' . $field['val'] . '"';
  • 297.
    			    if( $meta ) $val = ' value="' . $meta . '"';
  • 298.
    			    echo '<td>';                
  • 299.
                    echo '<input type="text" class="colorpicker" id="'. $field['id'] .'" name="icy_meta[' . $field['id'] .']"' . $val . ' />';
  • 300.
                    echo '</td>';
  • 301.
    			    break;
  • 302.
    				
  • 303.
    			case 'checkbox':
  • 304.
    			    echo '<td>';
  • 305.
    			    $val = '';
  • 306.
                    if( $meta ) {
  • 307.
                        if( $meta == 'on' ) $val = ' checked="checked"';
  • 308.
                    } else {
  • 309.
                        if( $field['std'] == 'on' ) $val = ' checked="checked"';
  • 310.
                    }
  • 311.
     
  • 312.
                    echo '<input type="hidden" name="icy_meta['. $field['id'] .']" value="off" />
  • 313.
                    <input type="checkbox" id="'. $field['id'] .'" name="icy_meta['. $field['id'] .']" value="on"'. $val .' /> ';
  • 314.
    			    echo '</td>';
  • 315.
    			    break;
  • 316.
    		}
  • 317.
    		
  • 318.
    		echo '</tr>';
  • 319.
    	}
  • 320.
     
  • 321.
    	echo '</table>';
  • 322.
    }
  • 323.
     
  • 324.
    /**
  • 325.
     * Save custom Meta Box
  • 326.
     *
  • 327.
     * @param int $post_id The post ID
  • 328.
     */
  • 329.
    function icy_save_meta_box( $post_id ) {
  • 330.
     
  • 331.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  • 332.
    		return;
  • 333.
    	
  • 334.
    	if ( !isset($_POST['icy_meta']) || !isset($_POST['icy_meta_box_nonce']) || !wp_verify_nonce( $_POST['icy_meta_box_nonce'], basename( __FILE__ ) ) )
  • 335.
    		return;
  • 336.
    	
  • 337.
    	if ( 'page' == $_POST['post_type'] ) {
  • 338.
    		if ( !current_user_can( 'edit_page', $post_id ) ) return;
  • 339.
    	} else {
  • 340.
    		if ( !current_user_can( 'edit_post', $post_id ) ) return;
  • 341.
    	}
  • 342.
     
  • 343.
    	foreach( $_POST['icy_meta'] as $key=>$val ){
  • 344.
    		update_post_meta( $post_id, $key, stripslashes(htmlspecialchars($val)) );
  • 345.
    	}
  • 346.
     
  • 347.
    }
  • 348.
    add_action( 'save_post', 'icy_save_meta_box' );
  • 349.
     
  • 350.
    /**
  • 351.
     * Save image ids
  • 352.
     */
  • 353.
    function icy_save_images() {
  • 354.
     
  • 355.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  • 356.
    		return;
  • 357.
    	
  • 358.
    	if ( !isset($_POST['ids']) || !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'icy-ajax' ) )
  • 359.
    		return;
  • 360.
    	
  • 361.
    	if ( !current_user_can( 'edit_posts' ) ) return;
  • 362.
     
  • 363.
    	$ids = strip_tags(rtrim($_POST['ids'], ','));
  • 364.
    	update_post_meta($_POST['post_id'], '_icy_image_ids', $ids);
  • 365.
     
  • 366.
    	// update thumbs
  • 367.
    	$thumbs = explode(',', $ids);
  • 368.
    	$thumbs_output = '';
  • 369.
    	foreach( $thumbs as $thumb ) {
  • 370.
    		$thumbs_output .= '<li>' . wp_get_attachment_image( $thumb, array(32,32) ) . '</li>';
  • 371.
    	}
  • 372.
     
  • 373.
    	echo $thumbs_output;
  • 374.
     
  • 375.
    	die();
  • 376.
    }
  • 377.
    add_action('wp_ajax_icy_save_images', 'icy_save_images');
  • 378.
     
  • 379.
     
  • 380.
    /*-----------------------------------------------------------------------------------*/
  • 381.
    /*	Register related Scripts and Styles
  • 382.
    /*-----------------------------------------------------------------------------------*/
  • 383.
     
  • 384.
    function icy_metabox_portfolio_scripts() {
  • 385.
        global $post;
  • 386.
        $wp_version = get_bloginfo('version');
  • 387.
        
  • 388.
    	wp_enqueue_script('media-upload');
  • 389.
    	wp_enqueue_script('iris');
  • 390.
    	wp_enqueue_script('wp-color-picker');
  • 391.
    	
  • 392.
    	if( version_compare( $wp_version, '3.4.2', '<=') ) {
  • 393.
    		// Using pre-WP3.5
  • 394.
    		wp_enqueue_script('thickbox');
  • 395.
    		wp_register_script('icy-upload', icy_URL .'/scripts/upload-button.js', array('jquery','media-upload','thickbox'));
  • 396.
    		wp_enqueue_script('icy-upload');
  • 397.
     
  • 398.
    		wp_enqueue_style('thickbox');
  • 399.
    		wp_enqueue_style('farbtastic');
  • 400.
    	}
  • 401.
    	
  • 402.
    	if( isset($post) ) {
  • 403.
    		wp_localize_script( 'jquery', 'icy_ajax', array(
  • 404.
    		    'post_id' => $post->ID,
  • 405.
    		    'nonce' => wp_create_nonce( 'icy-ajax' )
  • 406.
    		) );
  • 407.
    	}
  • 408.
    }
  • 409.
    add_action('admin_enqueue_scripts', 'icy_metabox_portfolio_scripts');
/** * Add a custom Meta Box * * @param array $meta_box Meta box input data */ function icy_add_meta_box( $meta_box ) { if( !is_array($meta_box) ) return false; // Create a callback function $callback = create_function( '$post,$meta_box', 'icy_create_meta_box( $post, $meta_box["args"] );' ); add_meta_box( $meta_box['id'], $meta_box['title'], $callback, $meta_box['page'], $meta_box['context'], $meta_box['priority'], $meta_box ); } /** * Create content for a custom Meta Box * * @param array $meta_box Meta box input data */ function icy_create_meta_box( $post, $meta_box ) { // set up for fallback to old way of doing things $wp_version = get_bloginfo('version'); if( !is_array($meta_box) ) return false; if( isset($meta_box['description']) && $meta_box['description'] != '' ){ echo '<p>'. $meta_box['description'] .'</p>'; } wp_nonce_field( basename(__FILE__), 'icy_meta_box_nonce' ); echo '<table class="form-table icy-metabox-table">'; foreach( $meta_box['fields'] as $field ){ // Get current post meta data $meta = get_post_meta( $post->ID, $field['id'], true ); echo '<tr><th><label for="'. $field['id'] .'"><strong>'. $field['name'] .'</strong> <span>'. $field['desc'] .'</span></label></th>'; switch( $field['type'] ){ case 'text': echo '<td><input type="text" name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['std']) .'" size="30" /></td>'; break; case 'textarea': echo '<td><textarea name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" rows="8" cols="5">'. ($meta ? $meta : $field['std']) .'</textarea></td>'; break; case 'file': if( version_compare($wp_version, '3.4.2', '>') ) { ?> <script> jQuery(function($) { var frame; $('#<?php echo $field['id']; ?>_button').on('click', function(e) { e.preventDefault(); // Set options for 1st frame render var options = { state: 'insert', frame: 'post' }; frame = wp.media(options).open(); // Tweak views frame.menu.get('view').unset('gallery'); frame.menu.get('view').unset('featured-image'); frame.toolbar.get('view').set({ insert: { style: 'primary', text: '<?php _e("Insert", "framework"); ?>', click: function() { var models = frame.state().get('selection'), url = models.first().attributes.url; $('#<?php echo $field['id']; ?>').val( url ); frame.close(); } } }); }); }); </script> <?php } // if version compare echo '<td><input type="text" name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['std']) .'" size="30" class="file" /> <input type="button" class="button" name="'. $field['id'] .'_button" id="'. $field['id'] .'_button" value="Browse" /></td>'; break; case 'images': if( version_compare($wp_version, '3.4.2', '>') ) { // Using Wp3.5+ ?> <script> jQuery(function($) { var frame, images = '<?php echo get_post_meta( $post->ID, '_icy_image_ids', true ); ?>', selection = loadImages(images); $('#icy_images_upload').on('click', function(e) { e.preventDefault(); // Set options for 1st frame render var options = { title: '<?php _e("Create Featured Gallery", "framework"); ?>', state: 'gallery-edit', frame: 'post', selection: selection }; // Check if frame or gallery already exist if( frame || selection ) { options['title'] = '<?php _e("Edit Featured Gallery", "framework"); ?>'; } frame = wp.media(options).open(); // Tweak views frame.menu.get('view').unset('cancel'); frame.menu.get('view').unset('separateCancel'); frame.menu.get('view').get('gallery-edit').el.innerHTML = '<?php _e("Edit Featured Gallery", "framework"); ?>'; frame.content.get('view').sidebar.unset('gallery'); // Hide Gallery Settings in sidebar // When we are editing a gallery overrideGalleryInsert(); frame.on( 'toolbar:render:gallery-edit', function() { overrideGalleryInsert(); }); frame.on( 'content:render:browse', function( browser ) { if ( !browser ) return; // Hide Gallery Settings in sidebar browser.sidebar.on('ready', function(){ browser.sidebar.unset('gallery'); }); // Hide filter/search as they don't work browser.toolbar.on('ready', function(){ if(browser.toolbar.controller._state == 'gallery-library'){ browser.toolbar.$el.hide(); } }); }); // All images removed frame.state().get('library').on( 'remove', function() { var models = frame.state().get('library'); if(models.length == 0){ selection = false; $.post(ajaxurl, { ids: '', action: 'icy_save_images', post_id: icy_ajax.post_id, nonce: icy_ajax.nonce }); } }); // Override insert button function overrideGalleryInsert() { frame.toolbar.get('view').set({ insert: { style: 'primary', text: '<?php _e("Save Featured Gallery", "framework"); ?>', click: function() { var models = frame.state().get('library'), ids = ''; models.each( function( attachment ) { ids += attachment.id + ',' }); this.el.innerHTML = '<?php _e("Saving...", "framework"); ?>'; $.ajax({ type: 'POST', url: ajaxurl, data: { ids: ids, action: 'icy_save_images', post_id: icy_ajax.post_id, nonce: icy_ajax.nonce }, success: function(){ selection = loadImages(ids); $('#_icy_image_ids').val( ids ); frame.close(); }, dataType: 'html' }).done( function( data ) { $('.icy-gallery-thumbs').html( data ); }); } } }); } }); // Load images function loadImages(images) { if( images ){ var shortcode = new wp.shortcode({ tag: 'gallery', attrs: { ids: images }, type: 'single' }); var attachments = wp.media.gallery.attachments( shortcode ); var selection = new wp.media.model.Selection( attachments.models, { props: attachments.props.toJSON(), multiple: true }); selection.gallery = attachments.gallery; // Fetch the query's attachments, and then break ties from the // query to allow for sorting. selection.more().done( function() { // Break ties with the query. selection.props.set({ query: false }); selection.unmirror(); selection.props.unset('orderby'); }); return selection; } return false; } }); </script> <?php // SPECIAL CASE: // std controls button text; unique meta key for image uploads $meta = get_post_meta( $post->ID, '_icy_image_ids', true ); $thumbs_output = ''; $button_text = ($meta) ? __('Edit Gallery', 'icy') : $field['std']; if( $meta ) { $field['std'] = __('Edit Gallery', 'icy'); $thumbs = explode(',', $meta); $thumbs_output = ''; foreach( $thumbs as $thumb ) { $thumbs_output .= '<li>' . wp_get_attachment_image( $thumb, array(32,32) ) . '</li>'; } } echo '<td> <input type="button" class="button" name="' . $field['id'] . '" id="icy_images_upload" value="' . $button_text .'" /> <input type="hidden" name="icy_meta[_icy_image_ids]" id="_icy_image_ids" value="' . ($meta ? $meta : 'false') . '" /> <ul class="icy-gallery-thumbs">' . $thumbs_output . '</ul> </td>'; } else { // Using pre-WP3.5 echo '<td><input type="button" class="button" name="' . $field['id'] . '" id="icy_images_upload" value="' . $field['std'] .'" /></td>'; } break; case 'select': echo'<td><select name="icy_meta['. $field['id'] .']" id="'. $field['id'] .'">'; foreach( $field['options'] as $key => $option ){ echo '<option value="' . $key . '"'; if( $meta ){ if( $meta == $key ) echo ' selected="selected"'; } else { if( $field['std'] == $key ) echo ' selected="selected"'; } echo'>'. $option .'</option>'; } echo'</select></td>'; break; case 'radio': echo '<td>'; foreach( $field['options'] as $key => $option ){ echo '<label class="radio-label"><input type="radio" name="icy_meta['. $field['id'] .']" value="'. $key .'" class="radio"'; if( $meta ){ if( $meta == $key ) echo ' checked="checked"'; } else { if( $field['std'] == $key ) echo ' checked="checked"'; } echo ' /> '. $option .'</label> '; } echo '</td>'; break; case 'color': if( array_key_exists('val', $field) ) $val = ' value="' . $field['val'] . '"'; if( $meta ) $val = ' value="' . $meta . '"'; echo '<td>'; echo '<input type="text" class="colorpicker" id="'. $field['id'] .'" name="icy_meta[' . $field['id'] .']"' . $val . ' />'; echo '</td>'; break; case 'checkbox': echo '<td>'; $val = ''; if( $meta ) { if( $meta == 'on' ) $val = ' checked="checked"'; } else { if( $field['std'] == 'on' ) $val = ' checked="checked"'; } echo '<input type="hidden" name="icy_meta['. $field['id'] .']" value="off" /> <input type="checkbox" id="'. $field['id'] .'" name="icy_meta['. $field['id'] .']" value="on"'. $val .' /> '; echo '</td>'; break; } echo '</tr>'; } echo '</table>'; } /** * Save custom Meta Box * * @param int $post_id The post ID */ function icy_save_meta_box( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !isset($_POST['icy_meta']) || !isset($_POST['icy_meta_box_nonce']) || !wp_verify_nonce( $_POST['icy_meta_box_nonce'], basename( __FILE__ ) ) ) return; if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) return; } else { if ( !current_user_can( 'edit_post', $post_id ) ) return; } foreach( $_POST['icy_meta'] as $key=>$val ){ update_post_meta( $post_id, $key, stripslashes(htmlspecialchars($val)) ); } } add_action( 'save_post', 'icy_save_meta_box' ); /** * Save image ids */ function icy_save_images() { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !isset($_POST['ids']) || !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'icy-ajax' ) ) return; if ( !current_user_can( 'edit_posts' ) ) return; $ids = strip_tags(rtrim($_POST['ids'], ',')); update_post_meta($_POST['post_id'], '_icy_image_ids', $ids); // update thumbs $thumbs = explode(',', $ids); $thumbs_output = ''; foreach( $thumbs as $thumb ) { $thumbs_output .= '<li>' . wp_get_attachment_image( $thumb, array(32,32) ) . '</li>'; } echo $thumbs_output; die(); } add_action('wp_ajax_icy_save_images', 'icy_save_images'); /*-----------------------------------------------------------------------------------*/ /* Register related Scripts and Styles /*-----------------------------------------------------------------------------------*/ function icy_metabox_portfolio_scripts() { global $post; $wp_version = get_bloginfo('version'); wp_enqueue_script('media-upload'); wp_enqueue_script('iris'); wp_enqueue_script('wp-color-picker'); if( version_compare( $wp_version, '3.4.2', '<=') ) { // Using pre-WP3.5 wp_enqueue_script('thickbox'); wp_register_script('icy-upload', icy_URL .'/scripts/upload-button.js', array('jquery','media-upload','thickbox')); wp_enqueue_script('icy-upload'); wp_enqueue_style('thickbox'); wp_enqueue_style('farbtastic'); } if( isset($post) ) { wp_localize_script( 'jquery', 'icy_ajax', array( 'post_id' => $post->ID, 'nonce' => wp_create_nonce( 'icy-ajax' ) ) ); } } add_action('admin_enqueue_scripts', 'icy_metabox_portfolio_scripts');

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter