WordPress: How to Get a Kaltura Video Widget to Render in Facebook Instant Articles

So in your posts you’re using the video widget from the Kaltura all in One Video Pack in tandem with the WordPress Instant Articles plugin.  You’re look at the debug information and you’re trying to understand why your videos aren’t showing up in the transformed markup.  This is because the video itself is not included in the source code of your post.  The widget works by inserting a few script tags in the markup which fire on page load and render your video player in an <iframe>.  So when the WordPress Instant Articles plugin transforms your markup the scripts haven’t fired and the video doesn’t exist in the markup yet.

 

If you’re like me you try playing with the transformer rules in the Instant Articles plugin’s settings to no avail.  Now you’re running out of time and you’re desperate.  Desperate times call for desperate measures.  Here is a function that overrides the Kaltura widget shortcode and replaces those script tags with an html5 video tag.

 


session->start($admin_secret, $user, KalturaSessionType::ADMIN, $partner_id);
if (!isset($ksession)) {
die("Could not establish Kaltura session. Please verify that you are using valid Kaltura partner credentials.");
}
//when creating the Kaltura Session it is important to specify that this KS should bypass entitlemets restrictions:

$api_url = ‘http://www.kaltura.com/api_v3/index.php?service=media&action=get&entryId=’;
$api_url .= $entryId;
$api_url .= ‘&ks=’;
$api_url .= $ksession;
$response = file_get_contents($api_url);
$response = new SimpleXMLElement($response);

$title = $response->result->name;
$description = $response->result->description;
$src_url = $response->result->dataUrl;
$thumb_url = $response->result->thumbnailUrl;
$html = ‘

‘;
$html .= ”;
$html .= ‘

‘;
$html .= $title;
$html .= ”;
$html .= ‘

‘;
$html .= ‘

‘;
$html .= ‘

‘;
$html .= $title;
$html .= ‘

‘;
$html .= ”;
$html .= $description;
$html .= ‘

‘;
$html .= ‘

‘;

$html .= ‘

‘;

return $html;

}
add_shortcode( ‘kaltura-widget’, ‘kaltura_widget_func’ );

add_action( ‘wp_enqueue_scripts’, ‘load_dashicons_front_end’ );
function load_dashicons_front_end() {
wp_enqueue_style( ‘dashicons’ );
}