I managed to do just that yesterday (couldn’t post it then)
This requires to modify the plugin files, which is not the best thing because on the next update they will be replaced. So very careful when that happens.
The file to change is class-instant-articles-post.php in the plugins folder.
First we have to add the Video class at the top of the file (where all the other classes are called). So, we have to add this:
use Facebook\InstantArticles\Elements\Video;
Then, we go to the to_instant_article function, in find the part of the code that André B. just posted.
$cover = $this->get_the_featured_image();
if ( $cover['src'] ) {
$image = Image::create()->withURL( $cover['src'] );
if ( isset( $cover['caption'] ) && strlen( $cover['caption'] ) > 0 ) {
$image->withCaption(
Caption::create()->withTitle( $cover['caption'] )
);
}
$header->withCover( $image );
}
And we replace it with this:
$cover = $this->get_the_featured_image();
if ( $cover['src'] ) {
if ($cover['type'] == 'video') {
//$image = $cover['src'];
$image = Video::create()->withURL( $cover['src'] )->enableComments()->enableLike();
} else {
$image = Image::create()->withURL( $cover['src'] );
}
if ( isset( $cover['caption'] ) && strlen( $cover['caption'] ) > 0 ) {
$image->withCaption(
Caption::create()->withTitle( $cover['caption'] )
);
}
$header->withCover( $image );
}
So, what it does is give you the option to add a video as cover. I added the enableComments and enableLike options, you can remove that if you don’t want it.
But this is not enought, since the plugin will still only use the featured image as cover. That makes makes sense, since WordPress doesn’t have the option to use a video as thumbnail, so I will asume that you have some custom code to add that in your post.
So, in functions.php of our theme, we add this:
function set_cover($cover_media,$post_id) {
// Cover image
$cover = HERE YOU HAVE TO GET YOU ATTACHMENT OBJECT, IMAGE OR VIDEO
if (strpos($cover['mime_type'], 'image') !== false) {
$url = $cover['url'];
$type = 'image';
} else if (strpos($cover['mime_type'], 'video') !== false) {
$url = $cover['url'];
$type = 'video';
}
$image_data = array (
'type' => $type,
'src' => $url,
'caption' => ''
);
return $image_data;
}
add_filter('instant_articles_featured_image','set_cover',100,9);
Here we hook on the ‘instant_articles_featured_image’ filter that the plugin has, and we create our own $image_data array. You will notice a “type” field there. This doesn’t existe in the plugin’s array. This is added to detect whether we are using an image or a video.
Hope this helps.
It would be cool there could be somehting like that in a future plugin update, so we can hook to the instant_articles_featured_image filter and just output a video