• I’m having an issue of a custom meta box I’m building based off of WooCommerce’s Product Gallery meta box. The meta box is not saving for some reason. I’ve tried some options from many different threads to no avail.

    Here is the class to add the actions for the meta boxes:

    class jxe_ims_admin_meta_boxes {
    		// Constructor
    		public function __construct() {
    			add_action( 'add_meta_boxes', array( $this, 'admin_scripts' ), 5  );
    			add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    			add_action( 'save_post', array ( $this, 'inventory_gallery_images::save' ) );
    		}
    		// Functions
    		public function admin_scripts() {
    			wp_register_script( 'gallery-metabox-ajax', plugins_url( '/lib/js/gallery-metabox-ajax.js', __FILE__ ) , array( 'jquery' ), null, true );
    			wp_register_style( 'gallery-metabox-style', plugins_url( '/lib/css/gallery-metabox-style.css', __FILE__ ), array(), null, 'all' );
    		}
    		public function add_meta_boxes() {
    			add_meta_box( 'jxe_ims-inventory-images', 'Inventory Gallery', 'inventory_gallery_images::output', 'jxe_ims', 'normal', 'high' );
    			wp_enqueue_script( 'gallery-metabox-ajax' );
    			wp_enqueue_style( 'gallery-metabox-style' );
    		}
    	}
    	new jxe_ims_admin_meta_boxes;

    And here is the class for the output and main functions:

    class inventory_gallery_images {
    		// Output the metabox HTML.
    		public static function output( $post ) {
    			global $post;
    
    			?>
    			<div id="inventory_images_container">
    				<ul class="inventory_images">
    					<?php
    						if ( metadata_exists( 'post', $post->ID, '_jxe_ims_image_gallery' ) ) {
    							$inventory_image_gallery = get_post_meta( $post->ID, '_jxe_ims_image_gallery', true );
    						} else {
    							// Backwards compat
    							$attachemnt_args = array(
    								'post_type'         => 'attachment',
    								'post_status'       => 'inherit',
    								'post_parent'       => $post_id,
    								'post_mime_type'    => 'image',
    								'posts_per_page'    => -1,
    								'order'             => 'ASC',
    								'fields'			=> 'ids',
    								'orderby'           => 'menu_order',
    								'meta_key'			=> '',
    								'meta_value'		=> 0
    							);
    							$attachment_ids = get_posts( $attachemnt_args );
    							$attachment_ids = array_diff( $attachment_ids, array( get_post_thumbnail_id() ) );
    							$inventory_image_gallery = implode( ',', $attachment_ids );
    						}
    
    						$attachments         = array_filter( explode( ',', $inventory_image_gallery ) );
    						$update_meta         = false;
    						$updated_gallery_ids = array();
    
    						if ( ! empty( $attachments ) ) {
    							foreach ( $attachments as $attachment_id ) {
    								$attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' );
    
    								// if attachment is empty skip
    								if ( empty( $attachment ) ) {
    									$update_meta = true;
    									continue;
    								}
    
    								echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
    									' . $attachment . '
    									<ul class="actions">
    										<li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete Image', 'jxe_ims' ) . '">' . __( 'Delete', 'jxe_ims' ) . '</a></li>
    									</ul>
    								</li>';
    
    								// rebuild ids to be saved
    								$updated_gallery_ids[] = $attachment_id;
    							}
    
    							// need to update inventory meta to set new gallery ids
    							if ( $update_meta ) {
    								update_post_meta( $post->ID, '_jxe_ims_image_gallery', implode( ',', $updated_gallery_ids ) );
    							}
    						}
    					?>
    				</ul>
    
    				<input type="hidden" id="inventory_image_gallery" name="inventory_image_gallery" value="<?php echo esc_attr( $inventory_image_gallery ); ?>" />
    
    			</div>
    			<p class="add_inventory_images hide-if-no-js">
    				<a href="#" data-choose="<?php esc_attr_e( 'Add Images to Inventory Gallery', 'jxe_ims' ); ?>" data-update="<?php esc_attr_e( 'Add to Gallery', 'jxe_ims' ); ?>" data-delete="<?php esc_attr_e( 'Delete Image', 'jxe_ims' ); ?>" data-text="<?php esc_attr_e( 'Delete', 'jxe_ims' ); ?>"><?php _e( 'Add Inventory Gallery Images', 'jxe_ims' ); ?></a>
    			</p>
    			<?php
    		}
    
    		// Save Meta Box Data
    		public static function save( $post_id, $post ) {
    			global $post;
    
    			$attachment_ids = isset( $_POST['jxe_ims_image_gallery'] ) ? array_filter( explode( ',', sanitize_text_field( $_POST['jxe_ims_image_gallery'] ) ) ) : array();
    
    			update_post_meta( $post_id, '_jxe_ims_image_gallery', implode( ',', $attachment_ids ) );
    		}
    	}

    The custom post type that this needs to be on is ‘jxe_ims’, and I do have another class that is saving a separate meta box with its own numerous fields. Any help will be appreciated. Is there something that I’m missing or need to change in order to get the gallery data to save?

  • The topic ‘Custom Meta Box Image Gallery Not Saving’ is closed to new replies.