Skip to content

Commit a7cd640

Browse files
committed
Add support for YouTube via URL as well as ID
1 parent e0fc6ff commit a7cd640

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ableplayer.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function able_player_parameters() {
280280
),
281281
'youtube-id' => array(
282282
'default' => '',
283-
'description' => __( '11-character YouTube ID', 'ableplayer' ),
283+
'description' => __( '11-character YouTube ID or a URL to a YouTube video page.', 'ableplayer' ),
284284
),
285285
'youtube-desc-id' => array(
286286
'default' => '',
@@ -408,10 +408,12 @@ function ableplayer_shortcode( $atts, $content = null ) {
408408
$o .= ' data-skin="' . $all_atts['skin'] . '"';
409409
}
410410
if ( ! empty( $all_atts['youtube-id'] ) ) {
411-
$o .= ' data-youtube-id="' . $all_atts['youtube-id'] . '"';
411+
$youtube_url = ableplayer_parse_youtube( $all_atts['youtube-id'] );
412+
$o .= ' data-youtube-id="' . $all_atts['youtube-id'] . '"';
412413
}
413414
if ( ! empty( $all_atts['youtube-desc-id'] ) ) {
414-
$o .= ' data-youtube-desc-id="' . $all_atts['youtube-desc-id'] . '"';
415+
$youtube_desc_url = ableplayer_parse_youtube( $all_atts['youtube-id'] );
416+
$o .= ' data-youtube-desc-id="' . $all_atts['youtube-desc-id'] . '"';
415417
}
416418
if ( ! empty( $all_atts['youtube-nocookie'] ) ) {
417419
$o .= ' data-youtube-nocookie="' . $all_atts['youtube-nocookie'] . '"';
@@ -438,6 +440,28 @@ function ableplayer_shortcode( $atts, $content = null ) {
438440
}
439441
add_shortcode( 'ableplayer', 'ableplayer_shortcode' );
440442

443+
/**
444+
* Parse the value of a YouTube shortcode attribute to extract YouTube ID if necessary.
445+
*
446+
* @param string $attr Value of the attribute.
447+
*
448+
* @param string YouTube video ID.
449+
*/
450+
function ableplayer_parse_youtube( $attr ) {
451+
$query = parse_url( $attr );
452+
if ( ! isset( $query['query'] ) ) {
453+
$replace = array( 'http://youtu.be/', 'https://youtu.be/' );
454+
455+
return str_replace( $replace, '', $attr );
456+
} else {
457+
parse_str( $query['query'], $results );
458+
459+
return $results['v'];
460+
}
461+
462+
return $attr;
463+
}
464+
441465
/**
442466
* Get unique ID for a specific Ableplayer instance.
443467
*

src/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ This example uses a shortcode to add a Vimeo player to the page, with two versio
107107

108108
== Changelog ==
109109

110-
= 1.2 =
110+
= 1.2.0 =
111111

112112
* Update Able Player to 4.5.0, while retaining 4.4.1 scripts.
113+
* Add support to pass YouTube URLs into shortcode as well as YouTube IDs.
113114
* Make scripts sensitive to SCRIPT_DEBUG or `wp_get_environment_type()` for easier debugging.
114115
* Add unminified versions of CSS.
115116
* Add filters to customize JS and CSS urls.

0 commit comments

Comments
 (0)