WP YouTube Live Shortcode

Below, you’ll find a detailed guide on how to add the WP YouTube Live Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP YouTube Live Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the WP YouTube Live Plugin and the shortcodes it provides:

Plugin Icon
WP YouTube Live

"WP YouTube Live is a dynamic WordPress plugin that allows seamless integration of live YouTube broadcasts into your website, enhancing user engagement and live interaction."

★★★★✩ (9) Active Installs: 2000+ Tested with: 5.9.8 PHP Version: false
Included Shortcodes:
  • [youtube_live]

WP YouTube Live [youtube_live] Shortcode

The WP YouTube Live shortcode is a powerful tool that allows embedding of live YouTube videos in WordPress. It enqueues necessary scripts and styles, fetches plugin settings, and sets shortcode attributes. It adjusts video dimensions, autoplay settings, and related video display. It also handles auto-refresh, fallback behavior, and messages. The shortcode can also modify refresh intervals via filters. Shortcode: [youtube_live]

Shortcode: [youtube_live]

Parameters

Here is a list of all possible youtube_live shortcode parameters and attributes:

  • width – Specifies the width of the YouTube live video.
  • height – Determines the height of the YouTube live video.
  • autoplay – If set to true, the video starts playing automatically.
  • show_related – If true, related YouTube videos are shown after the video ends.
  • js_only – If true, only JavaScript is used for the output.
  • ajaxUrl – The URL for the AJAX request.
  • auto_refresh – If true, the video refreshes automatically.
  • fallback_behavior – The action to take if the live stream isn’t available.
  • fallback_message – The message displayed if the live stream isn’t available.
  • fallback_playlist – The playlist displayed if the live stream isn’t available.
  • fallback_video – The video displayed if the live stream isn’t available.
  • refreshInterval – The interval at which the video refreshes, in seconds.

Examples and Usage

Basic example – Displays a YouTube live stream with default settings.

[youtube_live /]

Advanced examples

Display a YouTube live stream with a custom width and height, and set it to autoplay.

[youtube_live width="800" height="600" autoplay="true" /]

Display a YouTube live stream with custom fallback behavior and message when there’s no stream available.

[youtube_live fallback_behavior="playlist" fallback_message="Sorry, live stream is not available. Please enjoy our playlist instead." fallback_playlist="PL1234567890" /]

Display a YouTube live stream that doesn’t show related videos, and refreshes every 60 seconds.

[youtube_live show_related="false" auto_refresh="true" refreshInterval="60" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [youtube_live] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'youtube_live', 'output_youtube_live' );

Shortcode PHP function:

function output_youtube_live( $atts ) {
	// enqueue assets.
	wp_enqueue_script( 'wp-youtube-live' );
	wp_enqueue_style( 'wp-youtube-live' );
	wp_enqueue_script( 'youtube-iframe-api' );

	// get plugin settings.
	$settings = get_option( 'youtube_live_settings' );

	// get shortcode attributes.
	$shortcode_attributes = shortcode_atts(
		array(
			'width'             => $settings['default_width'],
			'height'            => $settings['default_height'],
			'autoplay'          => $settings['autoplay'],
			'show_related'      => $settings['show_related'],
			'js_only'           => false,
			'ajaxUrl'           => admin_url( 'admin-ajax.php' ),
			'auto_refresh'      => $settings['auto_refresh'],
			'fallback_behavior' => $settings['fallback_behavior'],
			'fallback_message'  => ( array_key_exists( 'no_stream_message', $settings ) ? $settings['no_stream_message'] : $settings['fallback_message'] ),
			'no_stream_message' => null,
			'fallback_playlist' => $settings['fallback_playlist'],
			'fallback_video'    => $settings['fallback_video'],
			'refreshInterval'   => apply_filters( 'wp_youtube_live_transient_timeout', '30' ),
		),
		$atts
	);

	// handle legacy parameter.
	if ( isset( $shortcode_attributes['no_stream_message'] ) ) {
		$shortcode_attributes['fallback_message'] = esc_attr( $shortcode_attributes['no_stream_message'] );
		unset( $shortcode_attributes['no_stream_message'] );
	}

	wp_add_inline_script( 'wp-youtube-live', 'var wpYouTubeLiveSettings = ' . wp_json_encode( $shortcode_attributes ), 'before' );

	return get_youtube_live_content( $shortcode_attributes );
}

Code file location:

wp-youtube-live/wp-youtube-live/wp-youtube-live.php

Conclusion

Now that you’ve learned how to embed the WP YouTube Live Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *