wp_playlist_shortcode( array $attr ): string
- Since
- 3.9.0
- Source
wp-includes/media.php:3192
Description
This implements the functionality of the playlist shortcode for displaying a collection of WordPress audio or video files in a post.
Compatibility
- WordPress
- since 3.9.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- Array of default playlist attributes.
$typestringdefault: 'audio'Type of playlist to display. Accepts 'audio' or 'video'.$orderstringdefault: 'ASC'Designates ascending or descending order of items in the playlist. Accepts 'ASC', 'DESC'.$orderbystringAny column, or columns, to sort the playlist. If $ids are passed, this defaults to the order of the $ids array ('post__in'). Otherwise default is 'menu_order ID'.$idintdefault: is the current post IDIf an explicit $ids array is not present, this parameter will determine which attachments are used for the playlist.$idsarraydefault: emptyCreate a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id.$excludearraydefault: emptyList of specific attachment IDs to exclude from the playlist.$stylestringdefault: 'light'Playlist style to use. Accepts 'light' or 'dark'.$tracklistbooldefault: trueWhether to show or hide the playlist.$tracknumbersbooldefault: trueWhether to show or hide the numbers next to entries in the playlist.$imagesbooldefault: trueShow or hide the video or audio thumbnail (Featured Image/post thumbnail).$artistsbooldefault: trueWhether to show or hide artist name in the playlist.
Return value
string- Playlist output. Empty string if the passed type is unsupported.
Performance profile
How much work a call to wp_playlist_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
- 68–201
- Plugin surface
- 2 hooks
- Called by
- 0
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 407.
Third-party callbacks on 'post_playlist', 'wp_playlist_scripts' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below wp_playlist_shortcode()
Further down the call graph this can also reach option, 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 · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_playlist_shortcode() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($attr) && empty($output) && empty($atts) && empty($args) && empty($attachments) | 68–72 | get_post(), apply_filters(), audio(), get_children() |
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && is_feed() | 74–79 | get_post(), apply_filters(), audio(), get_children(), is_feed() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && !current_user_can() | 77–81 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() | 81–83 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && is_feed() | 89–90 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed() |
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && empty($content_width) | 160–172 | get_post(), apply_filters(), audio(), get_children(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && !empty($content_width) | 164–176 | get_post(), apply_filters(), audio(), get_children(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && empty($content_width) | 170–182 | get_post(), apply_filters(), audio(), get_children(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && !empty($content_width) | 174–186 | get_post(), apply_filters(), audio(), get_children(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && empty($content_width) | 175–186 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && !empty($content_width) | 179–191 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean() |
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && empty($content_width) | 185–196 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean() |
1 further outcome, up to 201 instructions
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && !empty($content_width) | 189–201 | get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean() |
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 | 409 | 68–200 | 36 | 2 more instructions than PHP 8.5 |
| 8.5 | 407 | 68–201 | 36 | |
| 8.4 | 407 | 68–201 | 36 | |
| 8.3 | 407 | 68–201 | 36 | |
| 8.2 | 407 | 68–201 | 36 | |
| 8.1 | 407 | 68–201 | 36 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 409 | 68–201 | 36 |
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 · 2
2 hooks fire while wp_playlist_shortcode() runs, in this order:
- do_action( wp_playlist_scripts )actionline 3404 (+212 into the body)
Prints and enqueues playlist scripts, styles, and JavaScript templates.
Uses · 21
- get_post()Retrieves post data given a post ID or post object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- shortcode_atts()Combines user attributes with known attributes and fill in defaults when needed.
- get_posts()Retrieves an array of the latest posts, or posts matching the given criteria.
- get_children()Retrieves all children of the post parent ID.
- current_user_can()Returns whether the current user has the specified capability.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- is_feed()Determines whether the query is for a feed.
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
- wp_validate_boolean()Filters/validates a variable as a boolean.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- wp_check_filetype()Retrieves the file type from the file name.
Show all 21
- wp_get_mime_types()Retrieves the list of mime types and file extensions.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_get_attachment_id3_keys()Returns useful keys to use to lookup data from an attachment's stored metadata.
- get_post_thumbnail_id()Retrieves the post thumbnail ID.
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- wp_mime_type_icon()Retrieves the icon for a MIME type or attachment.
- esc_attr()Escaping for HTML attributes.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
Source code
function wp_playlist_shortcode( $attr ) { global $content_width; $post = get_post(); static $instance = 0; ++$instance; static $is_loaded = false; if ( ! empty( $attr['ids'] ) ) { // 'ids' is explicitly ordered, unless you specify otherwise. if ( empty( $attr['orderby'] ) ) { $attr['orderby'] = 'post__in'; } $attr['include'] = $attr['ids']; } /** * Filters the playlist output. * * Returning a non-empty value from the filter will short-circuit generation * of the default playlist output, returning the passed value instead. * * @since 3.9.0 * @since 4.2.0 The `$instance` parameter was added. * * @param string $output Playlist output. Default empty. * @param array $attr An array of shortcode attributes. * @param int $instance Unique numeric ID of this playlist shortcode instance. */ $output = apply_filters( 'post_playlist', '', $attr, $instance ); if ( ! empty( $output ) ) { return $output; } $atts = shortcode_atts( array( 'type' => 'audio', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'include' => '', 'exclude' => '', 'style' => 'light', 'tracklist' => true, 'tracknumbers' => true, 'images' => true, 'artists' => true, ), $attr, 'playlist' ); $id = (int) $atts['id']; if ( 'audio' !== $atts['type'] ) { $atts['type'] = 'video'; } $args = array( 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => $atts['type'], 'order' => $atts['order'], 'orderby' => $atts['orderby'], ); if ( ! empty( $atts['include'] ) ) { $args['include'] = $atts['include']; $_attachments = get_posts( $args ); $attachments = array(); foreach ( $_attachments as $key => $val ) { $attachments[ $val->ID ] = $_attachments[ $key ]; } } elseif ( ! empty( $atts['exclude'] ) ) { $args['post_parent'] = $id; $args['exclude'] = $atts['exclude']; $attachments = get_children( $args );Changelog
Introduced in 3.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.