wp_video_shortcode( array $attr, string $content = '' ): string|void
- Since
- 3.6.0
- Source
wp-includes/media.php:3652
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: emptyURL to the source of the video file.$heightintdefault: 360Height of the video embed in pixels.$widthintdefault: $content_width or 640Width of the video embed in pixels.$posterstringdefault: emptyThe 'poster' attribute for the<video>element.$loopstringdefault: emptyThe 'loop' attribute for the<video>element.$autoplaystringdefault: emptyThe 'autoplay' attribute for the<video>element.$mutedstringdefault: falseThe '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
- Scaling
- Scales with input
- Instructions
- 146–190
- Plugin surface
- 4 hooks
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 449.
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.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!get_post() && $override === "" && !is_admin() && empty($content_width) && empty($atts) && $library !== "mediaelement" | 146–164 | get_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–168 | get_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–163 | get_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–167 | get_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–180 | get_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–184 | get_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–186 | get_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–190 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 448 | 146–190 | 49 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 449 | 146–190 | 49 | |
| 8.4 | 449 | 146–190 | 49 | 24 fewer instructions than PHP 8.3 |
| 8.3 | 473 | 151–203 | 49 | |
| 8.2 | 473 | 151–203 | 49 | |
| 8.1 | 473 | 151–203 | 49 | |
| 7.4 | 473 | 151–203 | 49 |
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:
- apply_filters( wp_video_shortcode_override )filterline 3674 (+22 into the body)
Filters the default video shortcode output.
- apply_filters( wp_video_shortcode_library )filterline 3772 (+120 into the body)
Filters the media library used for the video shortcode.
- apply_filters( wp_video_shortcode_class )filterline 3808 (+156 into the body)
Filters the class attribute for the video shortcode output container.
- apply_filters( wp_video_shortcode )filterline 3896 (+244 into the body)
Filters the output of the video shortcode.
Uses · 24
- get_post()Retrieves post data given a post ID or post object.
- get_the_ID()Retrieves the ID of the current item in the WordPress Loop.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_video_extensions()Returns a filtered list of supported video formats.
- shortcode_atts()Combines user attributes with known attributes and fill in defaults when needed.
- is_admin()Determines whether the current request is for an administrative interface page.
- wp_check_filetype()Retrieves the file type from the file name.
- wp_get_mime_types()Retrieves the list of mime types and file extensions.
- esc_url()Checks and cleans a URL.
- esc_html()Escaping for HTML blocks.
- wp_enqueue_script()Enqueues a script.
- get_attached_media()Retrieves media attached to the passed post.
Show all 24
- wp_get_attachment_url()Retrieves the URL for an attachment.
- did_action()Retrieves the number of times an action has been fired during the current request.
- wp_enqueue_style()Enqueues a CSS stylesheet.
- remove_query_arg()Removes an item or items from a query string.
- set_url_scheme()Sets the scheme for a URL.
- wp_parse_url()A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.
- add_query_arg()Retrieves a modified URL query string.
- absint()Converts a value to non-negative integer.
- wp_validate_boolean()Filters/validates a variable as a boolean.
- esc_attr()Escaping for HTML attributes.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_mediaelement_fallback()Provides a No-JS Flash fallback as a last resort for audio / video.
Used by · 3
- WP_Widget_Media_Video::render_media()Render the media on the frontend.
- edit_form_image_editor()Displays the image and editor in the post editor
- prepend_attachment()Wraps attachment in paragraph tag before content.
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.
Signature, return type and hooks compared across 5 parsed releases.
string|void to string|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.