wppaste
WordPress

wp_video_shortcode( array $attr, string $content = '' ): string|void

Since
3.6.0
Source
wp-includes/media.php:3621
Builds the Video shortcode output.

Description

This implements the functionality of the Video Shortcode for displaying WordPress mp4s in a post.

Compatibility

WordPress
since 3.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attrarray
Attributes of the shortcode.
  • $srcstringdefault: empty

    URL to the source of the video file.
  • $heightintdefault: 360

    Height of the video embed in pixels.
  • $widthintdefault: $content_width or 640

    Width of the video embed in pixels.
  • $posterstringdefault: empty

    The 'poster' attribute for the <video> element.
  • $loopstringdefault: empty

    The 'loop' attribute for the <video> element.
  • $autoplaystringdefault: empty

    The 'autoplay' attribute for the <video> element.
  • $mutedstringdefault: false

    The 'muted' attribute for the <video> element.
  • $preloadstringdefault: 'metadata'

    The 'preload' attribute for the <video> element.
  • $classstringdefault: 'wp-video-shortcode'

    The 'class' attribute for the <video> element.
$contentstringoptional
Shortcode content.Default: ''

Return value

string|void
HTML content to display video.

Performance profile

How much work a call to wp_video_shortcode() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
146–190

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 449.

Plugin surface
4 hooks

Third-party callbacks on 'wp_video_shortcode_override', 'wp_video_shortcode_library', 'wp_video_shortcode_class' run inside this call, and their cost is not bounded by anything here.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_video_shortcode() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts) && $library !== "mediaelement"146–164get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts)150–168get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_mediaelement_fallback(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts) && !did_action()150–163get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), did_action(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts) && !did_action()154–167get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), did_action(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_mediaelement_fallback(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts)164–180get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), remove_query_arg(), set_url_scheme(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts)168–184get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), remove_query_arg(), set_url_scheme(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_mediaelement_fallback(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts)169–186get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), wp_parse_url(), add_query_arg(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), apply_filters()
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts)173–190get_post(), apply_filters(), wp_get_video_extensions(), shortcode_atts(), is_admin(), apply_filters(), wp_parse_url(), add_query_arg(), apply_filters(), absint(), absint(), esc_url(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_mediaelement_fallback(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev448146–190491 fewer instruction than PHP 8.5
8.5449146–19049
8.4449146–1904924 fewer instructions than PHP 8.3
8.3473151–20349
8.2473151–20349
8.1473151–20349
7.4473151–20349

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while wp_video_shortcode() runs, in this order:

  1. apply_filters( wp_video_shortcode_override )filterline 3643 (+22 into the body)

    Filters the default video shortcode output.

  2. apply_filters( wp_video_shortcode_library )filterline 3741 (+120 into the body)

    Filters the media library used for the video shortcode.

  3. apply_filters( wp_video_shortcode_class )filterline 3777 (+156 into the body)

    Filters the class attribute for the video shortcode output container.

  4. apply_filters( wp_video_shortcode )filterline 3865 (+244 into the body)

    Filters the output of the video shortcode.

Uses · 24

Show all 24

Used by · 3

Source code

function wp_video_shortcode( $attr, $content = '' ) {	global $content_width;	$post_id = get_post() ? get_the_ID() : 0; 	static $instance = 0;	++$instance; 	/**	 * Filters the default video shortcode output.	 *	 * If the filtered output isn't empty, it will be used instead of generating	 * the default video template.	 *	 * @since 3.6.0	 *	 * @see wp_video_shortcode()	 *	 * @param string $html     Empty variable to be replaced with shortcode markup.	 * @param array  $attr     Attributes of the shortcode. See {@see wp_video_shortcode()}.	 * @param string $content  Video shortcode content.	 * @param int    $instance Unique numeric ID of this video shortcode instance.	 */	$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); 	if ( '' !== $override ) {		return $override;	} 	$video = null; 	$default_types = wp_get_video_extensions();	$defaults_atts = array(		'src'      => '',		'poster'   => '',		'loop'     => '',		'autoplay' => '',		'muted'    => 'false',		'preload'  => 'metadata',		'width'    => 640,		'height'   => 360,		'class'    => 'wp-video-shortcode',	); 	foreach ( $default_types as $type ) {		$defaults_atts[ $type ] = '';	} 	$atts = shortcode_atts( $defaults_atts, $attr, 'video' ); 	if ( is_admin() ) {		// Shrink the video so it isn't huge in the admin.		if ( $atts['width'] > $defaults_atts['width'] ) {			$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] );			$atts['width']  = $defaults_atts['width'];		}	} else {		// If the video is bigger than the theme.		if ( ! empty( $content_width ) && $atts['width'] > $content_width ) {			$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] );			$atts['width']  = $content_width;		}	} 	$is_vimeo      = false;	$is_youtube    = false;	$yt_pattern    = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';	$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; 	$primary = false;	if ( ! empty( $atts['src'] ) ) {		$is_vimeo   = ( preg_match( $vimeo_pattern, $atts['src'] ) );		$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); 		if ( ! $is_youtube && ! $is_vimeo ) {			$type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 			if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {				return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );			}		}

Changelog

Introduced in 3.6.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from string|void to string|null.verified against source
3.6.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.