Image_Import::set_featured_image( bool $refresh = false )


Parameters

$refresh

(Optional)

Default value: false


Return

(array|bool|void)


Source

File: includes/libs/image-import.class.php

	public function set_featured_image( $refresh = false ){
		if( !$this->video_post->video_id ){
			return;
		}

		Helper::debug_message(
			sprintf(
				'Preparing to import featured image for post ID #%d.',
				$this->video_post->get_post()->ID
			)
		);

		if( $refresh ){
			$result = $this->import_from_api();
		}else{

			$args = [
				'post_type' => 'attachment',
				'meta_key'  	=> 'video_thumbnail',
				'meta_value'	=> $this->video_post->video_id
			];

			if( !empty( $this->video_post->image_uri ) ){
				$args['meta_key'] = '__vimeo_image_uri';
				$args['meta_value']  = $this->video_post->image_uri;
			}

			// check if thumbnail was already imported
			$attachment = get_posts( $args );
			// if thumbnail exists, return it
			if( $attachment ){
				// set image as featured for current post
				set_post_thumbnail( $this->video_post->get_post()->ID, $attachment[0]->ID );

				Helper::debug_message(
					sprintf(
						'An existing attachment having ID %d was detected and was set as featured image for post ID %d.',
						$attachment[0]->ID,
						$this->video_post->get_post()->ID
					)
				);

				$result = [
					'post_id' 		=> $this->video_post->get_post()->ID,
					'attachment_id' => $attachment[0]->ID
				];
			}else{
				$image_url = end( $this->video_post->thumbnails );
				$result = $this->import_to_media( $image_url );

				if( isset( $result['attachment_id'] ) && !empty( $this->video_post->image_uri ) ){
					update_post_meta(
						$result['attachment_id'],
						'__vimeo_image_uri',
						$this->video_post->image_uri
					);
				}

			}
		}

		if( !$result ){
			Helper::debug_message(
				'Error, the featured image was not imported.'
			);
		}

		return $result;
	}

Start your video site now!

Manage and coordinate your Vimeo channels, albums or videos with your WordPress website. Perfect fit for membership, portfolio, online courses or any type of video collection.

Get Vimeotheque PRO!