Vendor Images Gallery

Hi all,

I’m trying to add the gallery function to Vendors, I’ve successfully done it by keeping all the fields as “images” but this removes the ability to upload a Featured Image / Vendor Profile Picture.

So to keep the ability to upload featured images, I have created the below code, this below code allows me to add on the same gallery function that Listings have to Vendors (uploading through the backend and through the frontend), except it doesn’t “save”/“store” the images to the Vendor profile. The images are uploaded to the media gallery though.

// Add Vendor Images Field 
add_filter( 
    'hivepress/v1/models/vendor/fields', 
    function( $fields ) {
        $fields['vendor_images'] = [
            'label'     => hivepress()->translator->get_string( 'images' ),
            'caption'   => hivepress()->translator->get_string( 'select_images' ),
            'type'      => 'attachment_upload',
            'multiple'  => true,
            'max_files' => 10,
            'formats'   => [ 'jpg', 'jpeg', 'png' ],
            '_model'    => 'attachment',
            '_relation' => 'one_to_many',
        ];
        return $fields;
    } 
);

// Add Vendor Images to Vendor Update
add_filter( 
    'hivepress/v1/forms/vendor_update', 
    function( $form ) {
        if ( isset( $form['fields'] ) ) {
            $form['fields']['vendor_images'] = [
                '_order' => 10,
            ];
        }
        return $form;
    } 
);

// Add Vendor Images Meta Box
add_filter( 
    'hivepress/v1/meta_boxes', 
    function( $meta_boxes ) {
        $meta_boxes['vendor_photos'] = [
            'title'  => "Gallery Images",
            'screen' => 'vendor',
            'model'  => 'vendor',
            'fields' => [
                'vendor_images' => [
                    'caption'   => hivepress()->translator->get_string( 'select_images' ),
                    'type'      => 'attachment_upload',
                    'multiple'  => true,
                    'max_files' => 10,
                    'formats'   => [ 'jpg', 'jpeg', 'png' ],
                    '_order'    => 10,
                ],
            ],
        ];

        return $meta_boxes;
    }
);

Any help would be great, thanks!

Hey, I’ve been trying to do this too. Does is display in place of the small round display picture for the vendor?

I managed to get the images gallery to work alongside the profile photo. Here’s the code I added to my custom plugin:

// Add Vendor Images Field 
add_filter( 
    'hivepress/v1/models/vendor/fields', 
    function( $fields ) {
        $fields['images'] = [
            'label'     => hivepress()->translator->get_string( 'images' ),
            'caption'   => hivepress()->translator->get_string( 'select_images' ),
            'type'      => 'attachment_upload',
            'multiple'  => true,
            'max_files' => 10,
            'formats'   => [ 'jpg', 'jpeg', 'png' ],
            '_model'    => 'attachment',
            '_relation' => 'one_to_many',
        ];
        return $fields;
    } 
);

// Add Vendor Images to Vendor Update
add_filter( 
    'hivepress/v1/forms/vendor_update', 
    function( $form ) {
        if ( isset( $form['fields'] ) ) {
            $form['fields']['images'] = [
                '_order' => 10,
            ];
        }
        return $form;
    } 
);

// Add Vendor Images Meta Box
add_filter( 
    'hivepress/v1/meta_boxes', 
    function( $meta_boxes ) {
        $meta_boxes['vendor_images'] = [
            'title'  => "Gallery Images",
            'screen' => 'vendor',
            'model'  => 'vendor',
            'fields' => [
                'images' => [
                    'caption'   => hivepress()->translator->get_string( 'select_images' ),
                    'type'      => 'attachment_upload',
                    'multiple'  => true,
                    'max_files' => 10,
                    'formats'   => [ 'jpg', 'jpeg', 'png' ],
                    '_order'    => 10,
                ],
            ],
        ];

        return $meta_boxes;
    }
);

// Add Featured Image Meta Box
add_action( 
    'do_meta_boxes', function() {
        
    }, 20
);

Also, you’ll need to go into /wp-content/plugins/hivepress/includes/models/class-vendor.php and add the following code before the end of the last } symbol:

/**
	 * Gets image IDs.
	 *
	 * @return array
	 */
	final public function get_images__id() {
		if ( ! isset( $this->values['images__id'] ) ) {
			// Get cached image IDs.
			$image_ids = hivepress()->cache->get_post_cache( $this->id, 'image_ids', 'models/attachment' );
	
			if ( is_null( $image_ids ) ) {
				$image_ids = [];
	
				// Get file formats.
				$formats = [ 'image' ];
	
				// Optionally include videos.
				if ( get_option( 'hp_listing_allow_video' ) ) {
					$formats[] = 'video';
				}
	
				// Get image IDs, excluding the featured image.
				foreach ( get_attached_media( $formats, $this->id ) as $image ) {
					if ( $image->ID !== get_post_thumbnail_id( $this->id ) &&
						( ! $image->hp_parent_field || 'images' === $image->hp_parent_field ) ) {
						$image_ids[] = $image->ID;
					}
				}
	
				// Cache image IDs.
				hivepress()->cache->set_post_cache( $this->id, 'image_ids', 'models/attachment', $image_ids );
			}
	
			// Set field value.
			$this->set_images( $image_ids );
			$this->values['images__id'] = $image_ids;
		}

		// Debugging log
		error_log('Image IDs: ' . print_r($image_ids, true));
		
		return $this->fields['images']->get_value();
	}

With this, you should be able to set up an image gallery for vendors. The only thing I haven’t cracked yet is how to make the images show up in the meta box on the WordPress backend editor but that’s alright for me.

1 Like

I’ve attached my solution below!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.