On Wordpress, I'm trying to embed a YouTube video, then include links that jump to specific times in that video. So if I want to jump to 2:00, I can create a link that jumps to that point in the embedded video.
I found a similar question from a few years ago, but I believe the YouTube API has changed. I cannot find any plugins that can do this, so want to do it on my own if possible.
Suggestions?
Edit: Here is the code I've come up with so far. However, the only issue is that if the video hasn't been played yet, then seekTo seems to take a long time. I'm not sure if I should response to the onReady event, or another.
add_action( 'wp_enqueue_scripts', 'register_yt_frameapi' );
function register_yt_frameapi() {
wp_register_script( 'frameapi', 'https://www.youtube.com/iframe_api', array(), '1.0.0', true );
}
add_shortcode('ytlink', 'ytlink');
function ytlink($atts, $content = null)
{
wp_enqueue_script('frameapi');
$id = $atts['id'];
$time = $atts['time'];
$timeParts = explode(':', $time);
$seconds = 0;
for ($i = 0; count($timeParts); $i++)
{
$seconds += array_pop($timeParts) * 60 * $i;
}
return '<a href="javascript:void(0);" ' .
'onclick="var player = new YT.Player(\''.$id.'\', {events: {onReady: function () {player.seekTo('.$seconds.', true);}}});">'.
($content ?? $time) . '</a>';
}